重写 ToString() 方法,使用 Binding 时表现奇怪
overriding ToString() method, behaves strange with Binding
见下文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;
}
Class
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();
}
并且绑定更改为:
<DataGridTextColumn Header="Name" Binding="{Binding Path=Name,Mode=TwoWay}" />
但是 DataTrigger
和 Column
绑定的行为不同。有人可以解释为什么以及如何吗?为什么 DataTrigger
更改后不起作用?
<DataGridTextColumn Header="Name" Binding="{Binding Path=Name,Mode=TwoWay}" />
DataGridTextColumn
获取一个 CellContent
实例并调用 ToString()
来显示它。它显示 Value
,但如果路径中没有 .Value
,则不会应用数据网格单元格中的编辑。
<DataTrigger Binding="{Binding IsTrend}" Value="True" >
DataTrigger
获取一个 CellContent
实例并使用参数 "True"
调用 Equals()
。但是 CellContent 对象不等于 "True"
.
如果我覆盖 Equals
,DataTrigger 工作
public override bool Equals(object obj)
{
return Value.ToString() == (string) obj;
}
见下文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;
}
Class
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();
}
并且绑定更改为:
<DataGridTextColumn Header="Name" Binding="{Binding Path=Name,Mode=TwoWay}" />
但是 DataTrigger
和 Column
绑定的行为不同。有人可以解释为什么以及如何吗?为什么 DataTrigger
更改后不起作用?
<DataGridTextColumn Header="Name" Binding="{Binding Path=Name,Mode=TwoWay}" />
DataGridTextColumn
获取一个 CellContent
实例并调用 ToString()
来显示它。它显示 Value
,但如果路径中没有 .Value
,则不会应用数据网格单元格中的编辑。
<DataTrigger Binding="{Binding IsTrend}" Value="True" >
DataTrigger
获取一个 CellContent
实例并使用参数 "True"
调用 Equals()
。但是 CellContent 对象不等于 "True"
.
如果我覆盖 Equals
,DataTrigger 工作
public override bool Equals(object obj)
{
return Value.ToString() == (string) obj;
}