简体   繁体   English

属性设置器会修剪从LINQ分配的值,但在WPF数据网格中不会这样显示

[英]Property setter trims a value assigned from LINQ, but isn't displayed as such in WPF datagrid

I'm using LINQ to ODBC to project the results of a query, saved to an XML file, into a custom object. 我正在使用LINQ to ODBC将查询结果(保存到XML文件)投影到自定义对象中。 The custom object has a string property, "PayFrequency." 自定义对象具有字符串属性“ PayFrequency”。 Its setter trims the value assigned to it, if the value isn't null. 如果值不为null,则它的设置器将修剪分配给它的值。 I've opened my XML file and confirmed this works (the property value is trimmed). 我已经打开了XML文件并确认此文件有效(属性值被修剪)。

However, when the custom object is deserialized and displayed in a WPF datagrid, the DataGridComboBoxColumn column value is blank for my property. 但是,当自定义对象反序列化并显示在WPF数据网格中时,我的属性的DataGridComboBoxColumn列值为空白。 The column's ItemsSource is bound to a StaticResource of string, which includes the value assigned to my custom object's PayFrequency property. 列的ItemsSource绑定到字符串的StaticResource,该字符串包括分配给我的自定义对象的PayFrequency属性的值。 So, why doesn't my value show in the datagrid? 那么,为什么我的价值没有显示在数据网格中?

The PayFrequency property: PayFrequency属性:

    [XmlElement(IsNullable = true)]
    public string PayFrequency
    {
        get { return this.payFrequency; }
        set
        {
            if (value != this.payFrequency)
            {
                this.payFrequency = (value ?? value.Trim());
                NotifyPropertyChanged("PayFrequency");
            }
        }
    }

The WPF datagrid column: WPF datagrid列:

<DataGridComboBoxColumn
    Header="Pay Frequency"
    SelectedItemBinding="{Binding XLGOCommission.PayFrequency, Mode=TwoWay}"
    ItemsSource="{Binding Source={StaticResource xlgoPayFrequencyStrings}}" />

The WPF datagrid column's ItemsSource: WPF datagrid列的ItemsSource:

   <x:Array Type="{x:Type sys:String}" x:Key="xlgoPayFrequencyStrings">
        <sys:String>APM</sys:String>
        <sys:String>APQ</sys:String>
        <sys:String>OD</sys:String>
        <sys:String>NP</sys:String>
    </x:Array>

An important point: If I trim the value in my LINQ query, rather than letting my custom object's setter property handle it, the value displays correctly in the WPF datagrid column. 重要的一点:如果我在LINQ查询中修剪该值,而不是让自定义对象的setter属性对其进行处理,则该值会正确显示在WPF datagrid列中。 But, like I said, when I look at the custom object serialized to XML, there's no whitespace in the property value. 但是,就像我说的那样,当我查看序列化为XML的自定义对象时,属性值中没有空格。

Here's my band-aid fix (excerpt from my LINQ query): 这是我的创可贴修复程序(摘录自我的LINQ查询):

PayFrequency = xcom["pay_freq"].ToString().Trim()

Thanks for any help! 谢谢你的帮助! I'd much rather encapsulate the manipulation of values assigned my custom object in the object itself, rather than "cleaining up" values elsewhere! 我宁愿将对分配给我的自定义对象的值的操作封装在对象本身中,而不是“清白”其他地方的值!

This line is not correct. 这行是不正确的。 It will only try to trim value if it's null, which will generate a runtime exception... 它只会尝试修剪value null的值,这将生成运行时异常...

this.payFrequency = (value ?? value.Trim());

One way to trim it only if it's not null is to use the ternary operator instead... 仅当它不为null时修剪它的一种方法是改用三元运算符...

if (value != this.payFrequency)
{
    this.payFrequency = (value == null ? "" : value.Trim());
    NotifyPropertyChanged("PayFrequency");
}

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

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