简体   繁体   English

WPF-外部更改时,datagrid中的值无法更新转换器

[英]WPF - Value inside datagrid fails to update converter when changed externally

Turns out it's surprisingly hard to bubble up property changes from items inside an ObservableCollection - by default it only raises changed events when the collection itself is modified, not the items within it. 事实证明,很难从ObservableCollection内的项目中冒出属性更改的难度-默认情况下,它仅在修改集合本身时才引发更改的事件,而不会引发其中的项目。

This SO answer (to another of my questions on the subject) suggested that one way round the problem was to use a converter: 这样的回答(针对我在这个问题上的另一个问题)表明,解决问题的一种方法是使用转换器:

WPF MVVM - Datagrid does not update changed properties of child items WPF MVVM-Datagrid不会更新子项的更改属性

And for one of my scenarios, where the style of an item is dependent on its value and that value can be changed via the user editing the DataGrid, it works beautifully. 对于我的一种情况,项目的样式取决于其值,并且可以通过用户编辑DataGrid来更改该值,因此它的工作效果非常好。

However, in the same DataGrid it's possible to update another field by pressing a button - the button is in the same ViewModel, but it doesn't belong to the datagrid itself. 但是,在同一个DataGrid中,可以通过按一个按钮来更新另一个字段-该按钮在同一个ViewModel中,但它不属于datagrid本身。 I tried a similar trick to get this to work: 我尝试了类似的技巧来使它起作用:

<DataGridTextColumn Header="Status" Binding="{Binding Status}" IsReadOnly="True">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="Background">
                <Setter.Value>
                    <Binding Converter="{StaticResource BookingBackgroundConverter}" />
                </Setter.Value>
            </Setter>
        </Style>
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>

And the converter: 和转换器:

public class BookingBackgroundConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        Booking b = (Booking)value;

        switch(b.Status)
        {
            case "Booking" :
                return Brushes.LightBlue;
            case "Booked" :
                return Brushes.LightGreen;
            default :
                return Brushes.Transparent;
        }
    }

    public object ConvertBack(object value, Type TargetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Incidentally I was surprised that the value going into the Converter turned out to be a Booking - that's what the row is bound to, not the *column - I was expecting to get the string value of Status. 顺带一提,我很惊讶进入Converter的值竟然是Booking-那是绑定的内容,而不是* column-我期望得到Status的字符串值。 Might be useful to know why that is, as it may have a bearing on the answer. 知道为什么会有用,因为它可能与答案有关。

Anyway, here's the button click command that changes the Status: 无论如何,这是更改状态的按钮单击命令:

private void BookCount()
{
    SelectedBooking.Status = "Booking";
    rep.SaveChanges();
    OnPropertyChanged("");
}

SelectedBooking is a property that holds the currently selected row. SelectedBooking是保存当前所选行的属性。 And I'm firing an empty OnPropertyChanged in desperation to try and get this to update. 我无奈地射击了一个空的OnPropertyChanged,尝试更新它。

But no joy - the converter code doesn't get hit until the View reloads. 但是,没有喜悦-在重新加载View之前,转换器代码不会被点击。 Why does the converter approach work for datagrid edits but not button clicks, and what can I do to get the button click to visually update the status column? 为什么转换器方法适用于数据网格编辑而不适用于按钮单击,我该怎么办才能使按钮单击以可视方式更新状态列?

You change Status but you bind whole Booking object which means it will trigger only when whole object changes and not when any property from within that object changes. 您更改Status但绑定整个Booking对象,这意味着它仅在整个对象更改时才触发,而不是在该对象中的任何属性更改时才触发。 Bind Status instead 绑定Status

<Binding Path=Status, Converter="{StaticResource BookingBackgroundConverter}" />

and change converter to work with your booking status instead of Booking object. 并更改转换器以使用您的预订状态而不是Booking对象。 If you need to pass multiple values use IMultiValueConverter with MultiBinding 如果您需要传递多个值,请使用IMultiValueConverterMultiBinding

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

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