本文介绍了重写ToString()方法,使用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;
}

/ p>

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 从每个绑定我覆盖 ToString()方法。

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

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

,Binding更改为:

and Binding is changed as:

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

但是对于 DataTrigger 绑定。有人可以解释为什么和如何?为什么 DataTrigger 在更改后不起作用?

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

推荐答案

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

DataGridTextColumn 需要一个 CellContent 实例并调用 ToString()来显示它。它将显示,但不应用datagrid单元格中的路径编辑中的 .Value

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 需要一个 CellContent 实例并调用等于(),参数True。但是CellContent对象不等于True

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

如果我覆盖 / code>,DataTrigger工作

if I override Equals, DataTrigger works

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

这篇关于重写ToString()方法,使用Binding表现奇怪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-14 04:57