数据未更新的数据绑定问题

时间:2012-03-14 20:04:14

标签: c# .net wpf silverlight data-binding

我有一个数据网格,我不得不在后面的代码中添加列,因为我不知道在运行之前列是什么。 ResultsGrid是数据网格。

ResultsGrid.Columns.Add(new DataGridTextColumn()
            {
                Header = columnName,
                Binding = new Binding("results[" + columnName + "]"),
                CanUserSort = true,
                Visibility = buildingFieldList.Single(x => x.FieldName == columnName).Visibility
            });

buildingFieldList包含将使用的所有列的列表。它是一个ObservableCollection,包含我的自定义类型DBField的对象。我已经删除了不重要的东西并粘贴了下面的课程:

public class DBField : INotifyPropertyChanged
{
private bool _Enabled;
private Visibility _Visibility;

public bool Enabled 
{
    get { return _Enabled; }
    set
    {
        _Enabled = value;

        if (_Enabled)
            _Visibility = System.Windows.Visibility.Visible;
        else
            _Visibility = System.Windows.Visibility.Collapsed;

        OnPropertyChanged("Visibility");
        OnPropertyChanged("Enabled");
    }
}

public Visibility Visibility
{
    get { return _Visibility; }
    set
    {
        _Visibility = value;

        if (_Visibility == System.Windows.Visibility.Visible)
            _Enabled = true;
        else
            _Enabled = false;

        OnPropertyChanged("Enabled");
        OnPropertyChanged("Visibility");
    }
}

public event PropertyChangedEventHandler PropertyChanged;


protected void OnPropertyChanged(string name)
{
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(name));
    }
}
}

我有一个复选框,当单击时,修改启用的属性,因此也修改_Visibility并调用OnPropertyChanged。我期望发生的是该列现在已经崩溃了。但是,它并没有改变。有什么想法吗?

3 个答案:

答案 0 :(得分:0)

您将列上的visibility属性设置为静态值,而不是绑定。

Visibility = buildingFieldList.Single(x => x.FieldName == columnName).Visibility

您需要将Visibility属性绑定到对象的visibility属性。

答案 1 :(得分:0)

如果您希望在Visibility更改时更新DataGridTextColumn的DBField.Visibility,则必须使用绑定:

Binding binding = new Binding("Visibility");
binding.Source = buildingFieldList.Single(x => x.FieldName == columnName);

DataGridTextColumn column = 
    new DataGridTextColumn()    
    {    
        Header = columnName,    
        Binding = new Binding("results[" + columnName + "]"),    
        CanUserSort = true,    
    };

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

要完全删除DBField.Visibility并直接绑定到Enabled属性并使用BooleanToVisibilityConverter,这可能是一个好主意:

Binding binding = new Binding("Enabled");
binding.Source = buildingFieldList.Single(x => x.FieldName == columnName);
binding.Converter = new BooleanToVisibiltyConverter();

答案 2 :(得分:0)

这个问题比我想象的要棘手一些。克莱门斯为我走了正确的道路,但我没有看到银光有DataGridColumn.VisibilityProperty。我发现其他人遇到了问题,因为这个属性也没有曝光。为了解决这个问题,他们从专栏扩展并创建了自己的类。我也做了以下几点:

public class ExtendedDataGridTextColumn : DataGridTextColumn
{
public ExtendedDataGridTextColumn() : base() { }

public Visibility ColumnVisibility
{
    get { return (Visibility)GetValue(ColumnVisibilityProperty); }
    set { SetValue(ColumnVisibilityProperty, value); }
}

public static readonly DependencyProperty ColumnVisibilityProperty =
    DependencyProperty.Register(
    "ColumnVisibility",
    typeof(Visibility),
    typeof(ExtendedDataGridTextColumn),
    new PropertyMetadata(ColumnVisibilityChanged));

private static void ColumnVisibilityChanged(object sender, DependencyPropertyChangedEventArgs args)
{
    ExtendedDataGridTextColumn column = sender as ExtendedDataGridTextColumn;
    if (args.NewValue != args.OldValue)
    {
        column.Visibility = (Visibility)args.NewValue;
    }
}
}

然后我将这些自定义列添加到我的数据网格中:

Binding testBind = new Binding();
testBind.Mode = BindingMode.TwoWay;
testBind.Source = buildingFieldList.Single(x => x.FieldName == columnName);
testBind.Path = new PropertyPath("Visibility");

ExtendedDataGridTextColumn temp = new ExtendedDataGridTextColumn()
    {
    Header = columnName,
    Binding = new Binding("results[" + columnName + "]"),
    CanUserSort = true
    };

BindingOperations.SetBinding(temp, ExtendedDataGridTextColumn.ColumnVisibilityProperty, testBind);

ResultsGrid.Columns.Add(temp);

感谢Clemens帮助我。我希望这也有助于其他人。