简体   繁体   English

如何将WPF数据网格内的依赖项对象绑定到可观察的集合

[英]How to bind a dependency object inside a wpf datagrid to a observable collection

Background: I have a wpf datagrid that has a custom class ( CustomTextBox ) that inherits from a TextBox . 背景:我有一个wpf数据网格,它具有一个自定义类( CustomTextBox ),该类继承自TextBox I have created a dependency object ( ValueProperty ) in this custom class so I can bind to it. 我已经在这个自定义类中创建了一个依赖对象( ValueProperty ),因此我可以绑定到它。 I also have a observable collection that I use as a DataSource for the DataGrid . 我还有一个可观察的集合,用作DataGridDataSource

Issue: The problem is that when I bind my dependency property of my custom class to a public property in the observable collection, nothing is showing in the DataGrid . 问题:问题是,当我将自定义类的依赖项属性绑定到可观察集合中的公共属性时, DataGrid没有任何显示。

Here is the xaml for the DataGrid TemplateColumn where the binding happens: 这是发生绑定的DataGrid TemplateColumnxaml

<DataGridTemplateColumn Header="Drawing Location"  Width="240" CellStyle="{StaticResource dataGridCellStyle}">
      <DataGridTemplateColumn.HeaderTemplate>
        <DataTemplate>
            <Label Content="Drawing Location" Width="230" HorizontalContentAlignment="Center"/>
        </DataTemplate>
    </DataGridTemplateColumn.HeaderTemplate>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <CustomTextBox:CustomTextBox x:Name="ftbDrawingX" Value="{Binding DrawingLocationX, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                                             Style="{StaticResource textBox}" Width="85" Behavior="DistanceCombinedFtIn"/>
                <Label Content=","/>
                <CustomTextBox:CustomTextBox x:Name="ftbDrawingY" Value="{Binding DrawingLocationY, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                                             Style="{StaticResource textBox}" Width="85" Behavior="DistanceCombinedFtIn"/>
            </StackPanel>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

And here is my dependency property in the custom class: 这是自定义类中的依赖项属性:

public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(double), typeof(CustomTextBox), 
    new FrameworkPropertyMetadata(0D, FrameworkPropertyMetadataOptions.Journal | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(ValueProperty_OnTextPropertyChanged)));

public double Value
{
    get {return (double)this.GetValue(ValueProperty);}    
    set
    {

    }
}

In the code behind: 在后面的代码中:

private ObservableCollection<CustomItem> data = new ObservableCollection<CustomItem>();    
this.dgTakeoffDataGrid.ItemsSource = data;

The CustomItem object implements INotifyPropertyChanged and has two properties: DrawingLocationX and DrawingLocationY . CustomItem对象实现INotifyPropertyChanged并具有两个属性: DrawingLocationXDrawingLocationY

Can anyone please take a look and tell me what I have done wrong. 任何人都可以看看我是否做错了。 My DataGrid comes up empty, I know my issue is with binding to a dependency object. 我的DataGrid空了,我知道我的问题是绑定到依赖项对象。 Any help is appreciated here. 感谢您的帮助。

EDIT: I removed all logic from the setter property of Value, and I have added a callback method which handles the changes to the dependency object, and also updates it's Text property. 编辑:我从Value的setter属性中删除了所有逻辑,并添加了一个回调方法来处理对依赖对象的更改,还更新了它的Text属性。

private static void ValueProperty_OnTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs eventArgs)
    {
        CustomTextBox box = d as CustomTextBox;
        if (box != null)
        {
            box.value = Conversions.RoundLeastSignificant(Conversions.RoundLeastSignificant((double)eventArgs.NewValue));
            box.Text = Conversions.FormatReadTextValue((double)eventArgs.NewValue, box.behavior, box.acadDocument);
        }
    }

I have managed to resolve this issue. 我设法解决了这个问题。 The solution is in my last comment: The solution being to remove all the logic from the setter of the property, and handle setting Value in a CallBack method defined when you register the dependency property. 解决方案是我的最后一条评论:解决方案是从属性的设置器中删除所有逻辑,并处理在注册依赖项属性时定义的CallBack方法中的设置Value。

This is the general solution. 这是一般的解决方案。 Hope it helps others as well. 希望它也能帮助其他人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM