|
Hello All,
I am stuck in Wpf datgrid Cell updation.
I want the backcolor of a cell change when the value in the cell increases or decreases accordingly. like if the value increases the cell backcolor shud become green, and when the value decreases then its backcolor is red. Also when can get the increment or decrement value or the previous value so that i can know by how much it has decreased or increased.
Thanks
|
|
|
|
|
I'm going to assume that you are using MVVM here, and I'm going to show you how to do this without needing to use a Value converter. Some may argue that this isn't pure MVVM because I'm going to put a colour in the VM, but it's not one that I would really worry about too much. Anyway, here you go - add this type of code to your VM:
private const Brush greenBrush = new SolidColorBrush(Colors.Green);
private const Brush redBrush = new SolidColorBrush(Colors.Red);
private Brush currentBrush;
private int oldValue;
private int currentValue;
public Brush Background
{
get { return currentBrush; }
}
public int CurrentValue
{
get
{
return currentValue;
}
set
{
if (currentValue == value) return;
oldValue = currentValue;
currentValue = value;
if (oldValue < currentValue)
currentBrush = redBrush;
else
currentBrush = greenBrush;
OnChanged("CurrentValue");
OnChanged("CurrentBrush");
OnChanged("Difference");
}
}
public int Difference
{
get { return Math.Abs(currentValue - oldValue); }
} And that's it - bind the BackgroundValue to Background to get the background colour to respond to changes.
|
|
|
|
|
What's wrong with putting a color in the VM? That's kind of its job. To contain all the properties that the view needs to render the model. That's totally MVVM.
|
|
|
|
|
The reason you do not want the color in the ViewModel is that the viewmodel should not be concerned with View issues. The color is a something for the View since it is purely UI. This is just like having the ViewModel provide Visibility enumerations. The ViewModel should not have to worry about if you are in WPF or something else. Obviously there is nothing else besides Silverlight, which is the same, but that does not have to be the case.
|
|
|
|
|
That is the VM's job. To re-package the model for the view and to implement all the RelayCommands, etc. Visiblity, colors, widths, etc. are all appropriate for the VM. Otherwise, where do you draw the line? Say I have a listview. Now, you are saying the columns should not be in the VM because that is a view issue?
|
|
|
|
|
From Wikipedia:
View model: the view model is a “model of the view” meaning it is an abstraction of the view that also serves in mediating between the view and the model which is the target of the view data bindings. It could be seen as a specialized aspect of what would be a controller (in the MVC pattern) that acts as a converter that changes model information into view information and passes commands from the view into the model. The view model exposes public properties, commands, and abstractions. The view model has been likened to a conceptual state of the data as opposed to the real state of the data in the model.[10] The term "View model" is a major cause of confusion in understanding the pattern when compared to the more widely implemented MVC or MVP patterns. The role of the controller or presenter of the other patterns has been substituted with the framework binder (e.g., XAML) and view model as mediator and/or converter of the model to the binder.
Color is not an abstraction. You should be able to replace the View with a totally different implementation and still work. Color is part of the GUI, and not an abstraction of the Model, it is a requirement of the View. If it is decided that a different color should be used, that should not affect the ViewModel, any more than changing the position of the textbox should affect the ViewModel. Otherwise why not just go back to WinForms.
|
|
|
|
|
Clifford Nelson wrote: acts as a converter that changes model information into view information and
passes commands from the view into the model. The view model exposes public
properties, commands, and abstractions. The view model has been likened to a
conceptual state of the data as opposed to the real state of the data in the
model.
Sounds like the color would fit that description quite nicely IMO. Its an abstraction of a value change in this case. An up-tick is green and a down-tick is red. That's an abstraction.
Clifford Nelson wrote: Color is not an abstraction. You should be able to replace the View with a
totally different implementation and still work.
Having an up-tick color and a down-tick color exposed in the VM has no effect on swapping out the View whatsoever. Any View can either use it or not.
Clifford Nelson wrote: If it is decided that a different color should be used, that should not affect
the ViewModel, any more than changing the position of the textbox should affect
the ViewModel.
You are really reaching with that one . A color in this case is an abstraction of the up-tick / down-tick concept.
Anyways, I'd REALLY hate to have to maintain your code where every VM has dozens of value converters just for the heck of it. Seems like a nightmare to me . If you are working on a big UI where there are 50 views, you are really going to have 500+ converters??? Are you serious???
And if you REALLY want to be an anal MVVM / OOP purist... the color change doesn't belong in either place. That belongs in a refactored "Cell" object .
|
|
|
|
|
private void combogroup_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
------binded combobox
------
category ca = new category();
strCurCatGroup = combogroup.SelectedValue.ToString().Trim();
ca.changecategory(strCurCatGroup);
}
private void changecategory(string strCategoryGroup)
{
switch(---)
{
case "A":
groupbox1.visibility=Visibility.Hidden;
groupbox2.visibility=Visibility.Visible;
--
case "B":
groupbox1.visibility=Visibility.Visible;
groupbox2.visibility=Visibility.Hidden
}
|
|
|
|
|
Pray tell me sir, why have you dumped a bunch of code in our forum?
See that is a question, it is what we use when trying to get some help in a forum!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
so,can u help me how to do ?
|
|
|
|
|
Can I help you to do what you have not asked a question, explained what you want or described what your problem is. All you have done is dump some poorly written code into the forum. Read the guidelines at the top of the page on how to ask a question!
And please don't use txt speak.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hi,
I am creating an application in WPF using MVVM pattern. My requirement is to use a grid which should support dynamic row creation and columns are not static , it will be vary always depends on the selection.
Please help to achieve it.
Thanks in advance,
Selvam S
|
|
|
|
|
You should have not problem with creating a completely dynamic environment with WPF and a DataGrid. I have the following xaml:
<Window x:Class="DynamicGrid.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DynamicGrid"
Title="MainWindow"
Height="350"
Width="525">
<Window.DataContext>
<local:ViewModel />
</Window.DataContext>
<Grid>
<DataGrid AutoGenerateColumns="True"
ItemsSource="{Binding Children}"
HorizontalAlignment="Stretch"
Name="dataGrid1"
VerticalAlignment="Stretch" />
</Grid>
</Window>
with the following ViewModel:
class ViewModel
{
public ViewModel()
{
var temp = new BindingList<Class1>();
temp.Add(new Class1 {TypeA = "AAAA", TypeB = "BBBB"});
temp.Add(new Class1 { TypeA = "CCCC", TypeB = "CCCC" });
_children = temp;
}
private IEnumerable<object> _children;
public IEnumerable<object> Children
{
get { return _children; }
set { _children = value; }
}
}
The grid is automatically created, and can add new rows. Just use what ojects you need. If you need something even more dynamic, then can bind to a class that inherits from DynamicObject
|
|
|
|
|
Hi,
I am developing a WPF application in MVVM pattern.
I got stuck in closing a popup window. I will explain how I started my application.
I created Main window, Login User Control and New User window.
In my main window , I used content control to load my login user control. On clicking the new user button in login screen, I initiated new user window and then I filled user registration details in new user window and then clicked OK. Now my new user window must get closed and stay with main window but it is not happening.
But I am not able to close the new user window through my button click.
Kindly help me in this situation and provide me a best approach to solve the issue.
Thanks in advance
Regards,
Selvam S
|
|
|
|
|
Personally I use code behind to handle the click event of the button and set the DialogResult. Any processing of the data is done in the VM and is called from the code behind.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
This[^] is a fairly common solution to this problem.
|
|
|
|
|
If you look at the Prism samples[^], you will find a good solution that you could use to implement a self-closing popup dialog.
Just handle the mouse click event on the shell and you can close any popups that are open on the shell.
|
|
|
|
|
When I looked at WPF and MVVM I solved this by creating a base UserControl that handled showing itself on a form - either modally or not.
The ViewModel then has the ability of telling the view to close itself
Personally I think it is a far simpler solution than any others I have come across
You can see it in action if you read my articles (link in signature)
|
|
|
|
|
Hi there.
I have a control, that depending on its size and diferent parameters return me a value. Issue is that I need to do this in xalm and not in code, and do not know how to pass a tuple using xaml.
Now I have it by code:
Binding dataBinding = new Binding();
dataBinding.Source = this.ZoomMemoryArea.MemoryAreaData;
dataBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
dataBinding.Path = new PropertyPath("Size");
dataBinding.Converter = new BlockingRectangleMemorySizeConverter();
dataBinding.ConverterParameter = new Tuple<Canvas, IMemoryAreaData>(this.zoomCanvas, this.MemoryArea.MemoryAreaData);
dataBinding.Mode = BindingMode.OneWayToSource;
this.zoomControl.SetBinding(Control.WidthProperty, dataBinding);
And the ???? is what I really do not know how to do.
<pre lang="xml"><ContentControl.Width>
<Binding Mode="OneWayToSource" UpdateSourceTrigger="PropertyChanged" Converter="{StaticResource memorySizeConverter} ConverterParameter=???? Path="MemoryAreaData.Size" Source="ZoomMemoryArea">
</Binding></pre>
As for multiBinding, I can't use it as this is a OneWayToSource binding so I would not get more than one value.
Any sugestion would be much apreciated.
Regards
Forgot to add the converter
|
|
|
|
|
I have an XML file structured this way:
<Emulators>
<System>
<Name>Name1</Name>
<Kind name="lexplode"/>
<Kind name="sexplode"/>
<Kind name="dropbomb"/>
</System>
</Emulators>
If I want to read, in XAML, from <Name> I know that I have to do:
<TextBox Text="{Binding XPath=Name, UpdateSourceTrigger=PropertyChanged}"/>
But what if I want to read all the values under <Kind>> ?
And which control could receive them, a textbox, a listbox, ..?
Thanks a lot!!
modified 7-Sep-12 2:43am.
|
|
|
|
|
In order to get the attribute, you have to use a slightly different type of syntax. Rather than using XPath=name , you have to use XPath=@name . Now, that will only get you the value of the currently selected Kind item, so how do you actually get that item? Well, the answer is to use something like a ListBox to encapsulate the items.
<ListBox ItemsSource="{Binding Source={MyEmulatorsList, XPath=//Kind}" ItemTemplate="{StaticResource KindItemsTemplate}" /> Note: In this sample, I'm assuming that you have used an XmlDataProvider called MyEmulatorsList to point to this XML. So, where is the actual display of the name item? Well, that's provided by the ItemTemplate, which looks like this:
<DataTemplate x:Key="KindItemsTemplate">
<TextBlock Text="{Binding XPath=@name}" />
</DataTemplate>
|
|
|
|
|
Thanks for your reply!!
I think that I'd better expose the whole code.
XML file:
<Emulators>
<System>
<Gear>Name1</Gear>
<Kind name="att1"/>
<Kind name="att2"/>
<Kind name="att3"/>
</System>
</Emulators>
VB code:
Class MainWindow
Dim data As XmlDataProvider = New XmlDataProvider()
Private Sub MainWindow_Initialized(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Initialized
data.Source = New Uri(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\myapplication\Systems.xml")
data.XPath = "Emulators/System"
Grid1.DataContext = data
End Sub
End Class
XAML code:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525" Background="#FF4E4E64">
<Grid Name="Grid1">
<Grid.Resources>
<DataTemplate x:Key="KindTemplate">
<TextBlock Text="{Binding XPath=@name}" />
</DataTemplate>
</Grid.Resources>
<TextBox Text="{Binding XPath=Gear, UpdateSourceTrigger=PropertyChanged}" Height="23" HorizontalAlignment="Left" Margin="197,230,0,0" Name="TextBox3" VerticalAlignment="Top" Width="220" />
<ListBox ItemsSource="{Binding Source=data, XPath=//Kind}" ItemTemplate="{StaticResource KindTemplate}" Margin="261,51,12,149" />
</Grid>
</Window>
It doesn't work, I get a
System.Windows.Data Error: 44 : BindingExpression with XPath cannot bind to non-XML object.; XPath='//Kind' BindingExpression:Path=; DataItem='String' (HashCode=2037252866); target element is 'ListBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable') data
modified 7-Sep-12 13:08pm.
|
|
|
|
|
You are trying to bind to Emulators in your XPath, but your XML says Base.
|
|
|
|
|
You are trying to bind to Emulators in your XPath, but your XML says Base.
That's a typing mistake in my post (I've corrected it in my post)...but actually the XML file is correct..so the mistake doesn't come from here.
|
|
|
|
|
I think I see what the problem is. In the ListBox XAML, you don't need the Source=data (that's incorrect syntax, and besides, you already set the DataContext from the code-behind, so it's not needed. Take that part out, and it should work:
<ListBox ItemsSource="{Binding XPath=//Kind}" ItemTemplate="{StaticResource KindTemplate}"
Margin="261,51,12,149" />
|
|
|
|