|
If you want to bind to properties on the ViewModel, then your grid needs to be bound to the ViewModel. Each row of the grid is bound to one item in the ItemsSource, so that's where it's looking for "CellBackground"
|
|
|
|
|
I'm not sure I understand your answer, sorry.
The datagrid's ItemsSource is bound to the ViewModel (it is bound to a ViewModel's property of type ObservableCollection).
What I did now is:
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Background"
Value="{Binding Path=DataContext.CellBackground, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl, AncestorLevel=1}}"/>
</Style>
</DataGrid.CellStyle>
This works, but I'm sure that's not the most elegant solution...
|
|
|
|
|
Ahh, ok, I see where you're going with this now... I thought you wanted each cell's background to be based on the underlying data for that record, not that you wanted the entire grid to change color based on one property.
A better solution might be to have each "Person" object keep a reference to the ViewModel that owns it, like a "Parent" property. That way, you can bind to Parent.CellBackground...
Or if you're going to use FindAncestor (Which is a perfectly-legitimate method), you might want to source it to the DataGrid, not all the way up to the UserControl. That way, if you decide to move things around later, the grid stays modular.
|
|
|
|
|
Okay - perfect.
Thanks a lot!

|
|
|
|
|
What you need to do is bind the background property to the actual property that contains the Locked/Editable value, and then use a value converter to change the colour as appropriate. Here's a sample of the converter:
namespace MySample.Converters
{
using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;
public class StatusTextColorConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var brush = new SolidColorBrush(Colors.White);
if (value != null && value == "Locked")
{
brush = Colors.Yellow;
}
return brush;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
} Then you declare your converter as a reference in your XAML resources:
<conv:StatusTextColorConverter x:Key="statusTextColorConverter"/> (Here conv is a link to the converter namespace). Finally, you add it to your cell grid style:
<Setter Property="Background" Value="{Binding Path=PropertyContainingEditableText, Converter={StaticResource statusTextColorConverter}}}"/> By doing this, you remove the need to implement a UI based logic item in your ViewModel.
|
|
|
|
|
Thanks a lot for your answer, i'll try this out asap.
What do you mean specially with "UI based logic item"?
|
|
|
|
|
In this case, it means that you don't have to have a property in your ViewModel that returns a colour. The converter takes care of that for you.
|
|
|
|
|
I'm trying to make a StatusBar with four text items distributed evenly across the available width like this:
-----------------------------------------------------------------
| status1 | status2 | status3 | status4 |
-----------------------------------------------------------------
Instead I'm getting this:
-----------------------------------------------------------------
| status1 | status2 | status3 | status4 |
-----------------------------------------------------------------
...when I use the following code, which looks like it should do the trick:
<Window x:Class="WpfTests.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<StatusBar Grid.Row="0" Name="statusBar" VerticalAlignment="Bottom">
<StatusBarItem Content="status1" HorizontalAlignment="Stretch" />
<Separator />
<StatusBarItem Content="status2" HorizontalAlignment="Stretch" />
<Separator />
<StatusBarItem Content="status3" HorizontalAlignment="Stretch" />
<Separator />
<StatusBarItem Content="status4" HorizontalAlignment="Stretch" />
</StatusBar>
</Grid>
</Window>
What's wrong ?
Thanks,
J-L
|
|
|
|
|
I believe the StatusBar, by default, uses a DockPanel to handle its layout... Unless you specify otherwise, a DockPanel will left-dock all but the last item (Which will fill the remaining space).
You can change the layout by changing the ItemsPanel property of the StatusBar (It's just an ItemsControl):
<StatusBar ...>
<StatusBar.ItemsPanel>
<ItemsPanelTemplate>
...
</ItemsPanelTemplate>
</StatusBar.ItemsPanel>
...items...
</StatusBar>
If the separators aren't necessary, the easiest way is to use a UniformGrid, which will divide the space equally (Just set Rows = 1 and leave Columns blank). If you want to keep the separators, you could try using a regular Grid, and define columns for everything ("Auto" for separator columns, "*" for item columns).
|
|
|
|
|
It works perfectly, thanks a lot !
|
|
|
|
|
I have a Listbox of products and I bind all product names in a TextBlock inside the ListBox. For each product name I have a CheckBox next to it.
When I press a button "Check Selected products" I would like the MessageBox to list the checked products. I get an empty MessageBox and it is due to the property being null. I am not sure how to get around this problem. Any help would be appreciated.
MyView.xaml
<ListBox ItemsSource="{Binding AllProducts}" SelectionMode="Multiple">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="15"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<CheckBox VerticalAlignment="Center" Grid.Column="0"
IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ListBoxItem}}}" />
<TextBlock VerticalAlignment="Center" Grid.Column="1" Text="{Binding ProductName}" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
MyViewModel.cs
private string _isSelected;
public string IsSelected
{
get { return _isSelected; }
set { _isSelected = value;}
}
....
void MyCommandExecute()
{
if (this.IsSelected == null)
this.IsSelected = "property is null";
MessageBox.Show(this.IsSelected);
}
|
|
|
|
|
Well, you're binding IsSelected to the ListBoxItem itself, instead of to your ViewModel. Is that what you want to do?
|
|
|
|
|
I have a collection of products which are retrieved from a repository class. When the user see the list of products they should be able to check the products they want (just for testing I tried to display checked products with a MessageBox). I thought I could bind the CheckBox to the ListBox and set the Binding Path to IsSelected (a property in the ViewModel) so I could see which ones the user checked. But IsSelected is null when the getter property is called.
modified on Monday, May 9, 2011 2:47 PM
|
|
|
|
|
No, look at your binding for IsChecked... You're using a RelativeSource to bind it to the ListBoxItem. Get rid of that... If the IsSelected property is in the same place as ProductName, just use IsChecked="{Binding IsSelected}" .
If you find it's not updating properly, then use IsChecked="{Binding IsSelected,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
|
|
|
|
|
This was what I missed: If the IsSelected property is in the same place as ProductName
It works perfect now thanks!
|
|
|
|
|
Hi Ian,
I have now been trying to figure out how to "Select/Deselect All" checkboxes. My approach is the following:
Model
I have a IsSelected property in my Product class (in that class I also have productName, etc).
...
public string ProductName { get; set; }
...
private bool _isSelected;
public bool IsSelected
{
get
{
return _isSelected;
}
set
{
_isSelected = value;
OnPropertyChanged("IsSelected");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
ViewModel
...
void CheckAllCommandExecute()
{
foreach (Model.Product product in AllProducts)
{
if (product.IsSelected == false)
{
product.IsSelected = true;
}
else
product.IsSelected = false;
}
}
...
View
<ItemsControl ItemsSource="{Binding AllProducts}">
...
<CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
...
<TextBlock VerticalAlignment="Center" Grid.Column="1" Text="{Binding Path=ProductName}" />
...
<ButtonContent="Select/Deselect All" Command="{Binding Path=CheckAllCommand}" />
I thought OnPropertyChanged method would update the checkboxes either checked or unchecked. What do I do wrong?
Thanks in advance.
|
|
|
|
|
Assuming your command is executing properly, it should...
Does the Product class implement the INotifyPropertyChanged interface? You have to actually define the class as implementing the interface, not just have the event in there.
Are you sure the CheckAllCommandExecute method is executing? Set a breakpoint there... Remember that you have to use a CommandBinding to link the CheckAllCommand to the method.
|
|
|
|
|
Ian,
Adding this did the trick: "Does the Product class implement the INotifyPropertyChanged interface?"
Thank you very much for your help.
|
|
|
|
|
I have an application where I would like a treeview to behave like a tabcontrol. What I mean is that when the user clicks on a item in the treeview, to the right would appear something sort of like what you might see in Outlook. I am trying to avoid a tabcontrol simply because there will be lot's of tabs and the items have a hierarchy to them. For example There will be companies with adresses. Each company could have multiple addresses and I would like to have a treeview item of "addresses" and then each address will have a nickname like "main address" or "billing address" and I would like for the user to be able to click on this and then see it to the right. I was thinking of maybe using multiple grids and then showing and hiding but seems that that's not in the spirit of wpf and I don't know if it will even work.
|
|
|
|
|
I do something like this in one of my applications...
There are several ways to go about this, but I do mine with a DataTemplateSelector. Basically, my content pane is just a ContentControl (Actually, I think I'm using a Label, but I forget why), with its content bound to the selected item in the tree.
The DataTemplateSelector looks at the data object being displayed, and chooses which DataTemplate to use to display it (I have one for each type of item in the tree). The individual DataTemplates are just resources with standard names, so the selector code is just returning FindResource("DT_Something") as DataTemplate
|
|
|
|
|
I'm trying to implement this BoolToVisibility converter (answer #4)
http://stackoverflow.com/questions/3128023/wpf-booleantovisibilityconverter-that-converts-to-hidden-instead-of-collapsed-whe[^]
So I added the class:
namespace Abtech.Spares.UI.Classes
{
[ValueConversion(typeof(bool), typeof(Visibility))]
public sealed class BoolToVisibilityConverter : IValueConverter
{
public Visibility TrueValue { get; set; }
public Visibility FalseValue { get; set; }
public BoolToVisibilityConverter()
{
TrueValue = Visibility.Visible;
FalseValue = Visibility.Collapsed;
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (!(value is bool))
return null;
return (bool)value ? TrueValue : FalseValue;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (Equals(value, TrueValue))
return true;
if (Equals(value, FalseValue))
return false;
return null;
}
}
}
Then in the XAML I have
<UserControl x:Class="Abtech.Spares.UI.Views.ChildContentView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Abtech.Spares.UI.Classes;assembly=Abtech.Spares.UI"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="300">
<UserControl.Resources>
<local:BoolToVisibilityConverter
x:Key="BoolToHiddenConverter"
TrueValue="Visible"
FalseValue="Hidden" />
</UserControl.Resources>
<Grid>
.
.
.
.
and I'm getting a compilation error
Abtech.Spares.UI The tag 'BoolToVisibilityConverter' does not exist in XML namespace 'clr-namespace:Abtech.Spares.UI.Classes;assembly=Abtech.Spares.UI'. Line 13 Position 10.
You can see all the namespaces/references are correct. I don't see why this isn't working.
Anyone?
Everything makes sense in someone's mind
|
|
|
|
|
Well, can't give a definitive answer, but some possibles:
1) Maybe the Abtech.Spares.UI assembly didn't compile (Are there other errors that need to be fixed first?), in which case it's still trying to link to the previous version, which didn't have the new class.
2) If that XAML is in the same assembly as the converter, you don't need the ;assembly= part of the namespace reference. Try taking that part out, as namespace references can be a little picky.
3) Try doing a Clean + Rebuild, to make sure the assembly is compiling properly.
|
|
|
|
|
# 2 was the answer. I removed the assembly name and all works fine now.
Thank you!
Everything makes sense in someone's mind
|
|
|
|
|
|