2012-12-18 132 views
1

我想一個屬性添加到行的背景綁定行的背景...如何將屬性綁定到

的目標是隨機設置顏色到值「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

Value =「{Binding RelativeSource = {RelativeSource TemplatedParent},Path = DataContext.rowColor}」 嘗試在Setter上進行設置。沒有整個xaml,很難說。 –

+0

你的類是否實現INotifyPropertyChanged? – GameAlchemist

+0

沒有它不... – keno

回答

0

rowColor屬性不會引發PropertyChanged事件。

在OtherClass上實現INotifyPropertychanged,並在rowColor屬性的setter中引發PropertyChanged事件。

順便說一句:請堅持微軟命名屬性的方式:RowColor而不是rowColor。這可以防止混淆。

+0

我不知道,如果我知道了......我修改(不工作): (1)班OtherClass:INotifyPropertyChanged的 (2)this.OnPropertyChanged(「RowColor 「); (在set(get/set)中) (3)function public Event PropertyChangedEventHandler PropertyChanged; (3)XAML保持不變(exept namechange to rowColor => RowColor :-)) – keno

+0

請在問題中輸入確切的代碼,以便我們可以閱讀並回答。 –

+0

確定...完成:-)。 – keno

相關問題