我想一個屬性添加到行的背景綁定行的背景...如何將屬性綁定到
的目標是隨機設置顏色到值「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>
Value =「{Binding RelativeSource = {RelativeSource TemplatedParent},Path = DataContext.rowColor}」 嘗試在Setter上進行設置。沒有整個xaml,很難說。 –
你的類是否實現INotifyPropertyChanged? – GameAlchemist
沒有它不... – keno