我想将属性绑定到行的背景...
目标是将颜色随机设置为值'msgid'为空的行(使用+ - 10个RGB代码)。
注意:为什么我还要设置INotifyPropertyChanged? 我不明白......我不需要设置部分!?
我有什么:
在我的窗口类中:
OtherClass viewModel = new OtherClass();
this.DataContext = viewModel;
然后在OtherClass中:
class OtherClass : INotifyPropertyChanged
{
Brush color = new SolidColorBrush(Colors.Green);
public Brush RowColor
{
get { return color; }
set
{
color = value;
this.OnPropertyChanged("RowColor");
}
}
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged(string propName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(
this, new PropertyChangedEventArgs(propName));
}
}
XAML:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="536" Width="659" Loaded="Window_Loaded" Icon="/WpfApplication1;component/Images/dtc.ico">
<Window.Resources>
<XmlDataProvider x:Key="SmsData" XPath="/response">
</XmlDataProvider>
</Window.Resources>
<Grid Width="642">
<DataGrid ItemsSource="{Binding Source={StaticResource SmsData}, XPath=conv/sms}" AutoGenerateColumns="False" Name="dataGrid1" GridLinesVisibility="None" IsReadOnly="True" ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto" Margin="-5,13,4,244" BorderThickness="0" UseLayoutRounding="False" IsEnabled="True" SelectionChanged="dataGrid1_SelectionChanged">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding XPath=msgid}" Width="100" />
<DataGridTextColumn Binding="{Binding XPath=imsgid}" Width="100" />
<DataGridTextColumn Binding="{Binding XPath=recipient}" Width="100" />
<DataGridTextColumn Binding="{Binding XPath=datetime}" Width="120" />
<DataGridTextColumn Binding="{Binding XPath=message}" Width="225" />
</DataGrid.Columns>
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<DataTrigger Binding="{Binding XPath=msgid}" Value="{x:Null}">
<Setter Property="Background" Value="{Binding RowColor}" />
</DataTrigger>
</Style>
</DataGrid.RowStyle>
</DataGrid>
<RichTextBox Height="122" HorizontalAlignment="Left" Margin="402,311,0,0" Name="richTextBox1" VerticalAlignment="Top" Width="228" VerticalScrollBarVisibility="Visible" />
<Menu Height="23" HorizontalAlignment="Left" Name="menu1" VerticalAlignment="Top" Width="645">
<MenuItem Header="Menu">
<MenuItem Header="Exit" />
</MenuItem>
<MenuItem Header="Logs" />
<MenuItem Header="Help" />
</Menu>
<Image Height="48" HorizontalAlignment="Left" Margin="-10,251,0,0" Name="image1" Stretch="None" VerticalAlignment="Top" Width="317" Source="/WpfApplication1;component/Images/send%20sms.jpg" />
<Button Content="send messge" Height="23" HorizontalAlignment="Left" Margin="402,454,0,0" Name="button1" VerticalAlignment="Top" Width="228" Click="button1_Click" />
<Label Content="Number:" Height="28" HorizontalAlignment="Left" Margin="165,311,0,0" Name="label1" VerticalAlignment="Top" />
<Image Height="35" HorizontalAlignment="Left" Margin="300,252,0,0" Name="image2" Stretch="Fill" VerticalAlignment="Top" Width="338" Source="/WpfApplication1;component/Images/line.jpg" />
<TextBox Text="{Binding Path=Name2}" Height="23" HorizontalAlignment="Left" Margin="240,316,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
</Grid>
答案 0 :(得分:0)
rowColor属性不会引发PropertyChanged事件。
在OtherClass上实现INotifyPropertychanged并在rowColor属性的setter中引发PropertyChanged事件。
顺便说一句:请坚持微软命名属性的方式:RowColor而不是rowColor。这可以避免混淆。