简体   繁体   English

WPF MVVM是否与参数绑定?

[英]WPF MVVM Databinding with parameters?

So my previous question seem to not be answerable, so i'm going to give this a go with a suggestion of my own. 所以我先前的问题似乎无法回答,因此我将提出一个我自己的建议。

The functionality im looking for, is having a datagrid change the Foreground (or even background) of a cell, when the data in that cell has been edited. 我正在寻找的功能是,当一个单元格中的数据已被编辑时,使数据网格更改该单元格的前景(甚至背景)。

My Model looks something like this: 我的模型如下所示:

Public class Shipment : PropertyChangedBase
{
    #region Fields
    private ShippingService.Shipment.lbnshipment _localShipment;
    private ShippingService.Shipment.lbnshipment _originalShipment;
    #endregion

    #region Properties
    public int ShipmentID
    {
        get { return _localShipment.ShipmentID; }
        set
        {
            if(value != _localShipment.ShipmentID)
            {
                _localShipment.ShipmentID = value;
                NotifyOfPropertyChange(() => ShipmentID);
            }
         }
    } 

    public Shipment(ShippingServices.Shipment.lbnShipment localshipment)
    {
        _originalShipment = localshipment;
        _localShipment = localshipment;
    }

    //This Section is my best guess solution, but it just a guess
    public Color HasChanged(string Property)
    {
        switch(Property)
        {
           case "ShipmentID":
               if(_localShipment.ShipmentID != _originalShipment.ShipmentID)
               {
                  return Colors.Red;
               } else {
                  return Colors.Black;
               }
               break;
        }
    }
}

I've obviously stripped out most of the properties, and the HasChanged right now is pure myth, but what i'm hoping for is that somehow, i can bind a DataGridTextColumn Foreground(or background hopefully) to this HasChanged method, and somehow pass it which paramater is currently calling the method. 我显然已经剥夺了大多数属性,并且HasChanged现在是纯粹的神话,但是我希望的是通过某种方式,我可以将DataGridTextColumn前景(或希望是背景)绑定到此HasChanged方法,并以某种方式传递当前哪个参数正在调用该方法。

<DataGridTextColumn Header="ShipmentID" Binding="{Binding ShipmentID}" Foreground="{Binding HasChanged}" />

I'm hoping there is some clever way for me to allow the binding to identify which property has changed, much in the same way that IDataErrorInfo allows validation to be bound per property. 我希望我可以通过某种巧妙的方法来使绑定确定哪个属性已更改,这与IDataErrorInfo允许对每个属性绑定验证的方式非常相似。 Although i have no idea how that actually functions in the background. 虽然我不知道该如何在后台实际运行。

It calls out for a converter, no? 它要求转换器,不是吗? So your binding would look like this: 因此,您的绑定应如下所示:

<DataGridTextColumn.Foreground>
    <SolidColorBrush Color="{Binding Converter={StaticResource hasChangedConverter}, ConverterParameter='ShipmentID'}"/>
</DataGridTextColumn.Foreground>

And your converter would look like (stripping extraneous code): 您的转换器看起来像(剥离多余的代码):

class HasChangedConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var shipment = value as Shipment;
        var property = parameter as string;

        return shipment.HasChanged(property);
    }
}

UPDATE: Using single quotes on the converter parameter as shown above ought to work. 更新:如上所示,在转换器参数上使用单引号应该可以工作。 Failing that, you can use the extended format for the binding: 如果失败,则可以将扩展格式用于绑定:

<SolidColorBrush.Color>
    <Binding Converter="{StaticResource hasChangedConverter}" ConverterParameter="ShipmentID"/>
</SolidColorBrush.Color>

UPDATE II: ... and obviously we can't go around changing the background of DataGridTextColumn s, so change your column's XAML to something like the following: 更新II:...很显然,我们无法更改DataGridTextColumn的背景,因此将列的XAML更改为以下内容:

<DataGridTemplateColumn Header="ShipmentID">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding ShipmentID}">
                <TextBlock.Background> 
                    <SolidColorBrush Color="{Binding Path=ShipmentID, Converter={StaticResource HasChangedConv}}"/>
                </TextBlock.Background>
            </TextBlock>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

There are two options: 有两种选择:

  • If this Property thing is in UI binded to the ViewModel You can listen on changes of that property in the ViewModel and then changed the color accordingly. 如果该Property的事情是在绑定到视图模型UI可以监听在该属性的改变ViewModel ,然后相应地改变了颜色。

  • If not, use the Command System. 如果没有,请使用命令系统。 The ideal scenario would be: 理想的情况是:
    - The UI sends a Command to the ViewModel that something has changed. -UI将Command更改发送到ViewModel
    - The ViewModel executes the command, and changes the Color field. ViewModel执行命令,并更改Color字段。 UI needs to be informed that the color has changed. 需要通知UI颜色已更改。

Here's an implementation of ICommand interface: 这是ICommand接口的实现:

http://wpftutorial.net/DelegateCommand.html http://wpftutorial.net/DelegateCommand.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM