2

我陷入了一个必须动态创建数据网格列并且必须在 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);
        }
    }
}
4

1 回答 1

0

与您的绑定或价值转换器没有任何关系。你正在做这个任务:

Visibility = attribute.Visible

在视图中创建一个新列。

那是你的编译错误。Visibility 是 System.Windows.Visibility,attribute.Visible 是 bool。您不能将 Visibility 设置为布尔值。如果这个值是通过绑定设置的,那么你真的不需要手动设置它(实际上它会清除你的绑定)。

编辑:

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

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

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

第二次编辑: 我在上面的代码中看到了一些可能有问题的东西:

首先,当您设置绑定时,看起来您正在VisibilityProperty为您的 DependencyProperty 设置“”上的绑定。我不确定在您的观点中这是什么(可能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 绑定出现问题)。我不肯定这是一个问题,但绝对不需要设置可见性绑定。我在一个示例项目中使用了我在答案中提供的代码,并且我在上面提供的行中添加了所有代码(在取出导致编译错误的列初始化程序中的赋值之后。

于 2012-05-14T02:24:51.860 回答