WPF逗号分隔的多绑定问题 [英] WPF Comma-Separated Multi-Binding Issue

查看:94
本文介绍了WPF逗号分隔的多绑定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在尝试绑定到视图模型上存在的多个属性时遇到一些困难.绑定需要在RadGridView中的单元格中以逗号分隔的格式显示(类似于标准WPF DataGridView).

我绑定的所有属性都位于视图模型上的同一AddressDetails对象中. AddressDetails包含以下属性:Address1,Address2,Address3.

我研究了各种解决问题的方法,但是每种方法都有其缺点.任何帮助将不胜感激,我想尽量避免创建转换器,但是如果这是唯一可行的解​​决方案,那就这样吧.

解决方案1(多重绑定):
此解决方案使用多重绑定和StringFormat属性来显示数据.但是,无论地址属性是否包含任何数据,都会始终显示逗号.

Hello all,

I am experiencing some difficulties trying to bind to multiple properties, that exist on my view model. The bindings need to be shown in a comma-separated format within a cell, in a RadGridView (similar to the standard WPF DataGridView).

The properties that I am binding to all live within the same AddressDetails object on the view model. AddressDetails contains the properties: Address1, Address2, Address3.

I have looked into various solutions to the problem but each appear to have their disadvantages. Any help would be most appreciated, I would like to try steer clear of creating a converter, but if it is the only viable solution, then so be it.

Solution 1(Multi-binding):
This solution uses multi-bindings and the StringFormat property, to display the data. However the commas will always show, regardless if the address properties have any data.

<telerik:RadGridView ItemsSource="{Binding Locations, Mode=TwoWay}"

                     SelectedItem="{Binding SelectedLocation, Mode=TwoWay}">
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn Header="Reference" DataMemberBinding="{Binding Reference}"/>
        <telerik:GridViewDataColumn Header="Address Summary">
            <telerik:GridViewDataColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock>
                        <TextBlock.Text>
                           <MultiBinding StringFormat=" {0}, {1}, {2} ">
                               <Binding Path="AddressDetails.Address1"/>
                               <Binding Path="AddressDetails.Address2"/>
                               <Binding Path="AddressDetails.Address3"/>
                           </Multibinding>
                        </TextBlock.Text>
                    </TextBlock>
                </DataTemplate>
            </telerik:GridViewDataColumn.CellTemplate>
        </telerik:GridViewDataColumn>
    </telerik:RadGridView.Columns>
</telerik:RadGridView>





解决方案2(绑定到ToString覆盖):
基本上,这里发生的所有事情都是为AddressDetails对象提供的标准替代:





Solution 2(Bind to a ToString override):
Essentially all that is happening here is a standard override provided for the AddressDetails object:

public override string ToString()
{
    string result = string.Empty;
    string[] addressSummaryFields = (from a in new string[] { Address1, Address2, Address3, Address4, Address5, Country, PostCode }
                               where !string.IsNullOrEmpty(a)
                               select a).ToArray<string>();

    if (addressSummaryFields.Length > 0)
    {
        result = addressSummaryFields.Aggregate((o, n) => o = o + ", " + n);
    }

    return result;
}



该解决方案一直有效,直到通过网格下方的编辑器控件更改值为止,因为绑定是绑定到AddressDetails对象本身.这意味着,当Address1属性更改时,只有Address1属性发出通知,而不是整个AddressDetails对象.这意味着UI不会更新ToString重写绑定.



This solution works until the values are changed by an editor control below the grid, as the binding is to the AddressDetails object itself. Which means when the Address1 property changes, only the Address1 property sends out a notification as opposed to the entire AddressDetails object. This means the UI does not update the ToString overriden binding.

<telerik:RadGridView ItemsSource="{Binding Locations, Mode=TwoWay}"

                     SelectedItem="{Binding SelectedLocation, Mode=TwoWay}">
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn Header="Reference" 

                                    DataMemberBinding="{Binding Reference}"/>
        <telerik:GridViewDataColumn Header="Address Summary" 

                                    TextWrapping="WrapWithOverflow" 

                                    DataMemberBinding="{Binding AddressDetails}"/>
    </telerik:RadGridView.Columns>
</telerik:RadGridView>



预先感谢,
SeniorCrispy.



Thanks in advance,
SeniorCrispy.

推荐答案

在AddressDetails对象上,是否不能具有公开的只读属性.此属性可以将address1,address2,address3值格式化为字符串.如果更新了地址1、2或3(并且显然抛出了OnPropertyChangedEvent),您还可以将OnPropertyChangedEvent抛出到readonly属性上,从而触发您所关注的更改通知?
On your AddressDetails object, can you not have a readonly property that is exposed. This property could format the address1, address2 address3 values into a string. If address1, 2 or 3 is updated (and obviously throwing OnPropertyChangedEvent) you could also throw the OnPropertyChangedEvent on the readonly property thus firing the change notification you are after?


这篇关于WPF逗号分隔的多绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆