传递绑定到DataGridTextColumn的子类

时间:2012-02-23 15:58:21

标签: wpf data-binding custom-controls

我有一个数据网格,我希望为某些列提供自定义上下文菜单。我有以下正常工作

<DataGridTextColumn Binding="{Binding Path=Column1ViewModel.Value}"  Header="Column1">
                <DataGridTextColumn.CellStyle>
                    <Style TargetType="{x:Type DataGridCell}">
                        <Setter Property="ContextMenu">
                            <Setter.Value>
                                <ContextMenu>
                                      <TextBlock Grid.Row="0" Grid.Column="0">Valuation Method Type:</TextBlock>
                                      <ComboBox Grid.Row="0" Grid.Column="1" ItemsSource="{Binding Path=Column1ViewModel.Property1}" DisplayMemberPath="Value" SelectedValuePath="Key" SelectedValue="{Binding Column1ViewModel.ValuationMethodTypeId}"></ComboBox>

                                    </ContextMenu>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </DataGridTextColumn.CellStyle>
            </DataGridTextColumn>

但我不想为每个列重复此代码。理想情况下,我有一个继承自DataGridTextColumn的控件,以便我可以这样做:

<ContextMenuColumn Header="Column 1" CustomBinding="{Binding Path=Column1ViewModel" />
<ContextMenuColumn Header="Column 2" CustomBinding="{Binding Path=Column2ViewModel" />
etc

我创建了一个DataGridTextColumn的子类,它有一个名为CustomBinding的依赖项属性,但是没有设置绑定。我也不知道如何绑定自定义列中的控件,以便它们从传入的绑定中读取。

ContextMenuColumn的xaml看起来很像上面的DataGridTextColumn xaml,虽然我不知道绑定是如何工作的。代码隐藏具有以下内容:

public static readonly DependencyProperty CustomBindingProperty =
DependencyProperty.Register("CustomBinding", typeof(object), typeof(ContextMenuColumn), new PropertyMetadata(null, OnCustomBindingPropertyChanged));


private static void OnCustomBindingPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
    {

    }

public object CustomBinding
    {
        get { return GetValue(CustomBindingProperty ); } 
        set{ SetValue(CustomBindingProperty , value);}
    }

当我在OnCustomBindingPropertyChanged中设置断点时,它永远不会到达。

0 个答案:

没有答案