我怎样才能动态改变在WPF约束力DataGridTextColumn转换器? [英] How can I dynamically change the converter on DataGridTextColumn binding in WPF?

查看:132
本文介绍了我怎样才能动态改变在WPF约束力DataGridTextColumn转换器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不明白WPF和XAML,并继承了一些可怕的书面code,所以我可能会屠宰这一点,但在这里不用。

I really don't understand WPF and XAML, and inherited some terribly written code, so I may be butchering this, but here goes.

我继承的约束(在code后面)一个DataGrid,以Person对象,在必要的DataGridTextColumns在XAML指定的列表(presumably让造型)。

I inherited a DataGrid bound (in code behind) to a list of Person objects, where the necessary DataGridTextColumns are specified in XAML (presumably to allow styling).

<DataGrid x:Name="PersonGrid" ItemsSource="{Binding}" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Path=LastName}" MaxWidth="Infinity" MinWidth="150" Header="Last Name">
        <DataGridTextColumn Binding="{Binding Path=FirstName}" MaxWidth="Infinity" MinWidth="150" Header="First Name" />
        <DataGridTextColumn Binding="{Binding Path=DOB, StringFormat=\{0:MM/dd/yyyy\}}" MaxWidth="Infinity" MinWidth="200" Header="Date of Birth (MM/DD/YYYY)" />
        <DataGridTextColumn Binding="{Binding Path=Number}" MaxWidth="Infinity" MinWidth="150" Header="(P)erson Number" />
    </DataGrid.Columns>
    <DataGrid.DataContext>
        <dm:Person />
    </DataGrid.DataContext>
</DataGrid>

我想只显示该人最后初期,可选基于一个复选框的状态。

I would like to display just the person's last initial, optionally based on the state of a checkbox.

<CheckBox Name="ShowFullNames_CheckBox" Content="Show Full Names" IsChecked="False"/>

我能够连接一台变频器的姓氏在code绑定的背后,却得到一个错误(绑定它已被用于后不能更改。)当我尝试改变数据后转换器势必。

I was able to hook up a converter to the LastName Binding in code behind, but get an error ("Binding cannot be changed after it has been used.") when I try to change that converter after the data is bound.

我想,也许我也可以绑定的复选框状态器isChecked向ConverterParameter或一个Multibinding结合,但不能得到工作。

I thought that maybe I could also bind the checkbox IsChecked state to the ConverterParameter or one binding of a Multibinding, but couldn't get that to work.

<DataGridTextColumn MaxWidth="Infinity" MinWidth="150" Header="Last Name">
    <DataGridTextColumn.Binding>
        <MultiBinding Converter="myStringTruncator">
            <Binding Source="ShowFullNames_CheckBox" Path="IsChecked"/>
            <Binding Path="LastName"/>
        </MultiBinding>
    </DataGridTextColumn.Binding>
</DataGridTextColumn>

在myStringTruncator的转换方法,第一个结合只是充满了DependencyProperty.UnsetValue,而不是复选框的值。

In the Convert method of myStringTruncator, the first binding was just filled with DependencyProperty.UnsetValue, instead of the value of the checkbox.

有可能是一个非常简单的方法来做到这一点,我没有看到。任何想法?

There's probably a really simple way to do this that I'm not seeing. Any ideas?

推荐答案

您可以转换器添加到XAML绑定。

You can add the Converter to the Binding in XAML.

<DataGridTextColumn Binding="{Binding Path=LastName, Converter={StaticResource YourConverter}"
                    MaxWidth="Infinity"
                    MinWidth="150"
                    Header="Last Name">

但对于绑定复选框的状态,你将不得不使用这样的事情(未经测试)

But for binding the state of the checkbox you would have to use something like this (untested)

<DataGridTextColumn Header="Last Name">
  <DataGridTextColumn.Binding>
    <MultiBinding Converter="{StaticResource NameAndCheckBoxMultiValueConverter}">
      <Binding Path="LastName" />
      <Binding ElementName="myCheckBox" Path="IsChecked" />
    </MultiBinding>
  </DataGridTextColumn.Binding>
 </DataGridTextColumn>

和转换器:

  using System;
    using System.Globalization;
    using System.Windows.Data;

    namespace TestWpf
    {
        public class NameAndCheckBoxMultiValueConverter: IMultiValueConverter
        {
            public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
            {
            var lastName = values[0] as String;
            if(lastName != null)
            {
                var isChecked = (bool)values[1];
                if (isChecked)
                {
                    return lastName.Substring(0,1);
                }
                return lastName;
            }
            return null;
            }

            public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    }

这篇关于我怎样才能动态改变在WPF约束力DataGridTextColumn转换器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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