见下文XAML
:
<Window x:Class="TabControl.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TabControl"
Title="MainWindow" Height="300" Width="300"
xmlns:Interact="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}"
>
<Window.Resources>
<Style TargetType="{x:Type DataGridRow}" x:Key="myStyle">
<Style.Triggers>
<DataTrigger Binding="{Binding IsTrend.Value}" Value="True" >
<Setter Property="Background" Value="Gold"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<ScrollViewer>
<DataGrid ItemsSource="{Binding list}" x:Name="myGrid" RowStyle="{StaticResource myStyle}" >
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Path=Name.Value,Mode=TwoWay}" />
</DataGrid.Columns>
</DataGrid>
</ScrollViewer>
DataGrid
的来源
private ObservableCollection<dynamic> GetDynamicOrders2()
{
var retVal = new ObservableCollection<dynamic>();
for (int i = 0; i < 50; i++)
{
dynamic eo = new ExpandoObject();
eo.Name = new CellContent("Order" + i);
eo.IsTrend = new CellContent(i % 2 == 0);
retVal.Add(eo);
}
return retVal;
}
类
public sealed class CellContent : INotifyPropertyChanged
{
private object _value;
public object Value
{
get { return _value; }
set
{
_value = value;
OnPropertyChanged("Value");
}
}
public CellContent(object value)
{
Value = value;
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
要从每个绑定.Value
override
方法中删除ToString()
。
public override string ToString()
{
return Value.ToString();
}
和Binding更改为:
<DataGridTextColumn Header="Name" Binding="{Binding Path=Name,Mode=TwoWay}" />
但DataTrigger
和Column
绑定的行为方式不同。有人可以解释为什么和如何?为什么DataTrigger
在更改后无效?
答案 0 :(得分:2)
<DataGridTextColumn Header="Name" Binding="{Binding Path=Name,Mode=TwoWay}" />
DataGridTextColumn
获取CellContent
个实例并调用ToString()
来显示它。它会显示Value
,但在路径中没有.Value
,数据网格单元格中的编辑不会应用。
<DataTrigger Binding="{Binding IsTrend}" Value="True" >
DataTrigger
获取CellContent
个实例,并使用参数Equals()
调用"True"
。但是CellContent对象不等于"True"
。
如果我覆盖Equals
,DataTrigger工作
public override bool Equals(object obj)
{
return Value.ToString() == (string) obj;
}