有什么办法来暂时分离在WPF绑定? [英] Is there any way to temporarily detach a binding in WPF?

查看:78
本文介绍了有什么办法来暂时分离在WPF绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:

我有一个的ListView / GridView控件与几个列。在某些情况下,只有某些列被示出。由于没有为 GridViewColumns WPF中没有可见属性,我把我想隐藏零列的宽度。从外观上看,这达到了预期的效果(我实际上是修改了控件模板 GridViewColumnHeader ,以便它的用户不可能意外扩大任何隐藏的列)。

I have a ListView/GridView with several columns. In some situations, only some of the columns are shown. Since there is no Visible property for GridViewColumns in WPF, I set the width of the columns I want to hide to zero. Visually, this achieves the desired effect (and I actually modified the ControlTemplate for the GridViewColumnHeader so that it's impossible for the user to accidentally expand any hidden columns).

问题:

问题是,对于隐藏的列的绑定仍然在发挥作用,他们正试图查找该数据不存在。在这种情况下,它会导致一个 IndexOutOfRangeException ,因为它正试图查找该不会对数据表中存在的列名这势必。

The problem is that the bindings for the hidden columns are still in play, and they are trying to look up data that doesn't exist. In this case, it causes an IndexOutOfRangeException since it's trying to look up a column name that doesn't exist on the DataTable that it is bound to.

问:

我如何暂时禁用或分离为隐藏的列的绑定?还是请大家提出一个更好的解决方案,如果你有一个。

How can I temporarily disable or detach the binding for columns that are hidden? Or please suggest a better solution if you have one.

谢谢!

推荐答案

啊,我想我已经得到了它。 的IValueConverter 救援。

Ahh, I think I've got it. IValueConverter to the rescue.

这是我想出了万一别人有同样的问题的解决方案:

Here's the solution I came up with in case someone else has the same problem:

步骤1.创建一个转换器。

的IValueConverter 检查索引是否超出范围,如果是这样,则返回一个空字符串。请注意,我用的是转换器的参数来保存列名。

This IValueConverter checks whether the index was out of range, and if so, returns an empty string. Note that I use the converter's parameter to hold the column name.

public class DataRowViewToCellString : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        DataRowView row = (DataRowView)value;
        string columnName = (string)parameter;
        if (row.DataView.Table.Columns.Contains(columnName))
            return row[columnName].ToString();
        else
            return Binding.DoNothing;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return Binding.DoNothing;
    }
}

第2步:扔转换器转换成的DataTemplate

Step 2. Throw the converter into a DataTemplate.

<local_converters:DataRowViewToCellString
    x:Key="TaskWindow_DataRowViewToCellString" />

<DataTemplate
    x:Key="TaskWindow_Column4Template">
    <TextBlock
        Text="{Binding Converter={StaticResource TaskWindow_DataRowViewToCellString}, ConverterParameter=Column4}" />
</DataTemplate>

第3步:参照模板中的隐藏有时 GridViewColumn

Step 3. reference the template in the "sometimes hidden" GridViewColumn.

<ListView ... >
    <ListView.View>
        <GridView ... >
            ...
            <GridViewColumn
                Header="SometimesHiddenColumn"
                CellTemplate="{StaticResource TaskWindow_Column4Template}">
        </GridView>
    </ListView.View>
</ListView>

修改

更改的情况下转换器,其中列名超出范围的String.Empty每丹尼斯罗氏的建议Binding.DoNothing的返回值。

Change the return value of the converter in cases where the column name is out of range from string.Empty to Binding.DoNothing per Dennis Roche's suggestion.

这篇关于有什么办法来暂时分离在WPF绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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