overriding ToString() method, behaves strange with

2019-09-23 18:59发布

问题:

see below 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>

Source of 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));
    }
}

To remove .Value from every binding I override ToString() method.

public override string ToString()
{
    return Value.ToString();
}

and Binding is changed as:

<DataGridTextColumn Header="Name" Binding="{Binding Path=Name,Mode=TwoWay}" />    

But It's behaving differently for DataTrigger and Column Binding. Can someone explain why and How? Why is DataTrigger not working after the change?

回答1:

<DataGridTextColumn Header="Name" Binding="{Binding Path=Name,Mode=TwoWay}" />  

DataGridTextColumn takes a CellContent instance and calls ToString() to display it. It displays Value, but without .Value in the path edits in datagrid cells are not applied.

<DataTrigger Binding="{Binding IsTrend}" Value="True" >

DataTrigger takes a CellContent instance and calls Equals() with parameter "True". But CellContent object is not equal to "True".

if I override Equals, DataTrigger works

public override bool Equals(object obj)
{
    return Value.ToString() == (string) obj;
}