bool到System.Windows.Visibility(动态创建DataGridColumnHeaders时的绑定问题)

时间:2012-05-14 01:56:32

标签: c# xaml binding datagrid visibility

我陷入了一个必须动态创建datagrid列的场景,并且必须使用C#代码创建列。我在每个生成列的单独代码区域中都有一个复选框。该复选框确定特定列是隐藏还是可见。该复选框绑定到GameAttributes.Visible属性。但是,DataGrid Visibility属性的类型不同。我尝试使用BooleanToVisibilityConverter,但仍然收到编译错误(正如我所知)。 是否有任何有效的解决方法来解决这个问题?

我遇到的错误:

Cannot implicitly convert type 'bool' to 'System.Windows.Visibility'    

编辑:编译器错误已解决,但绑定似乎不适用于可见性。

XAML:

<DataGrid ItemsSource="{Binding}" 
              DockPanel.Dock="Top" 
              AutoGenerateColumns="False" 
              HorizontalAlignment="Stretch" 
              Name="GameDataGrid" 
              VerticalAlignment="Stretch" 
              CanUserAddRows="False" 
              CanUserDeleteRows="False" 
              CanUserResizeRows="False"
              IsReadOnly="True"
              >

查看:

GameAttributes.Add(new GameInfoAttributeViewModel() { Visible = true, Description = "Name", BindingName = "Name" });
GameAttributes.Add(new GameInfoAttributeViewModel() { Visible = false, Description = "Description", BindingName = "Description" });
GameAttributes.Add(new GameInfoAttributeViewModel() { Visible = false, Description = "Game Exists", BindingName = "GameExists" });
foreach (GameInfoAttributeViewModel attribute in GameAttributes)
{

    DataGridTextColumn column = new DataGridTextColumn
    {
        Header = attribute.Description,
        Binding = new Binding(attribute.BindingName),
    };

    Binding visibilityBinding = new Binding();
    visibilityBinding.Path = new PropertyPath("Visible");
    visibilityBinding.Source = attribute;
    visibilityBinding.Converter = new BooleanToVisibilityConverter();
    BindingOperations.SetBinding(column, VisibilityProperty, visibilityBinding);

    GameDataGrid.Columns.Add(column);

}

视图模型:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;

namespace DonsHyperspinListGenerator
{
    class GameInfoAttribute
    {
        public string Description { get; set; }
        public bool Visible { get; set; }
        public string BindingName { get; set; }
    }

    //todo: move to separate class
    class GameInfoAttributeViewModel : INotifyPropertyChanged
    {
        private GameInfoAttribute mGameInfo = new GameInfoAttribute();

        public string Description
        {
            get { return mGameInfo.Description; }
            set
            {
                if (mGameInfo.Description != value)
                {
                    mGameInfo.Description = value;
                    InvokePropertyChanged("Description");
                }
            }
        }

        public bool Visible
        {
            get { return mGameInfo.Visible; }
            set
            {
                if (mGameInfo.Visible != value)
                {
                    mGameInfo.Visible = value;
                    InvokePropertyChanged("Visible");
                }
            }
        }

        public string BindingName
        {
            get { return mGameInfo.BindingName; }
            set
            {
                if (mGameInfo.BindingName != value)
                {
                    mGameInfo.BindingName = value;
                    InvokePropertyChanged("BindingName");
                }
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;

        private void InvokePropertyChanged(string propertyName)
        {
            var e = new PropertyChangedEventArgs(propertyName);
            PropertyChangedEventHandler changed = PropertyChanged;
            if (changed != null) changed(this, e);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

与您的装订或价值转换器没有任何关系。你正在完成这项任务:

Visibility = attribute.Visible

在视图中创建新列。

那是你的编译错误。可见性是System.Windows.Visibility,而attribute.Visible是bool。您无法将可见性设置为布尔值。如果通过Binding设置此值,那么您实际上不需要手动设置它(实际上它将清除您的绑定)。

修改

以下是如何在后面的代码中设置绑定以使用值转换器的示例:

var binding = new Binding();
binding.Source = attribute;
binding.Path = new PropertyPath("Visible");
binding.Converter = (BooleanToVisibilityConverter)Resources["BoolToVisibilityConverter"];

BindingOperations.SetBinding(column, DataGridTemplateColumn.VisibilityProperty, binding);

第二次修改: 我在上面的代码中看到了一些可能存在问题的事情:

首先,当您设置绑定时,看起来您正在为DependencyProperty设置绑定“VisibilityProperty”。我不确定你的观点是什么(可能是UserControl.VisibilityProperty,或者某种东西)。您要设置的特定DependencyProperty位于DataGridTemplateColumn类型上,因此我相信您需要将其设置为DataGridTemplateColumn.VisibilityProperty

所以这一行:BindingOperations.SetBinding(column, VisibilityProperty, visibilityBinding);

变为:BindingOperations.SetBinding(column, DataGridTemplateColumn.VisibilityProperty, visibilityBinding);

另一件事是DataGridTextColumn的对象初始值设定项中的这一行:

Binding = new Binding(attribute.BindingName),

我不确定你在使用该行做了什么,但它可能会导致列的整个DataContext出现问题(这可能会导致Visibility绑定出现问题)。我不肯定这是一个问题,但是设置Visibility Binding肯定不需要 。我在我的答案中提供了一个示例项目中提供的代码,并且我在上面提供的行中添加了所有代码(在列初始化程序中取出导致编译错误的赋值之后。