将 DataGrid 的 CellTemplate 内容绑定到在模板化的自定义控件上定义的元素或依赖项属性



我在自定义控件中使用WPF的常规DataGrid。DataGrid的一个单元格模板内容应绑定到Textblock的Text或自定义控件上的DependencyProperty。(如果我能把它绑定到其中任何一个上,对我来说就足够了)

我尝试使用ElementName进行以下绑定,但没有成功。我一直得到一个DependencyProperty。UnsetValue-

<DataGridTemplateColumn Header="Test">
     <DataGridTemplateColumn.CellTemplate>
         <DataTemplate>
             <ContentPresenter>
                <ContentPresenter.Content>
                    <MultiBinding Converter="{StaticResource TextToSpecialTextblockConverter}">
                       <Binding Path="SomeTextOnTheViewModel"/>
                       <Binding ElementName="SearchBox" Path="Text" Mode="OneWay"/>
                    </MultiBinding>
                </ContentPresenter.Content>
             </ContentPresenter>
         </DataTemplate>
     </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

绑定到DependencyProperty也不起作用。

<DataGridTemplateColumn Header="Test">
     <DataGridTemplateColumn.CellTemplate>
         <DataTemplate>
             <ContentPresenter>
                <ContentPresenter.Content>
                    <MultiBinding Converter="{StaticResource TextToSpecialTextblockConverter}">
                       <Binding Path="SomeTextOnTheViewModel"/>
                       <Binding RelativeSource="{RelativeSource Mode=TemplatedParent}" Path="SomeDP" />
                    </MultiBinding>
                </ContentPresenter.Content>
             </ContentPresenter>
         </DataTemplate>
     </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

我希望有人能帮我!

谢谢!

如果属性Property是在控件的DataContextDataGrid上设置的视图模型上定义的,则此示例有效:

资源:

    <DataTemplate x:Key="template">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="Test"/>
            <TextBlock Text="{Binding Path=DataContext.Property,RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
        </StackPanel>
    </DataTemplate>

DataGrid

<DataGrid ItemsSource="{Binding Items}" ...>
    <DataGrid.Columns>
        <DataGridTemplateColumn CellTemplate="{StaticResource template}" />

DependencyProperty更改通知无法更新视图模型。

例如:

    public static readonly DependencyProperty TestProperty =
        DependencyProperty.Register("Test", typeof (string), typeof (DataGridComboBoxColumn), new PropertyMetadata(default(string), PropertyChangedCallback));
    public string Test
    {
        get { return (string) GetValue(TestProperty); }
        set { SetValue(TestProperty, value); }
    }
    private static void PropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
    {
        ((MyControl) dependencyObject).OnTestChanged();
    }
    private void OnTestChanged()
    {
        ((MyViewModel) theGrid.DataContext).Property = Test;
    }

为了使用模板绑定绑定到依赖属性Test,请使用此

<DataTemplate x:Key="template2">
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="Test: "/>
        <TextBlock Text="{Binding Path=Test, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type local:CustomControl1}}}"/>
    </StackPanel>
</DataTemplate>
<Style TargetType="{x:Type local:CustomControl1}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:CustomControl1}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <DataGrid ItemsSource="{TemplateBinding ItemsSource}" AutoGenerateColumns="True">
                        <DataGrid.Columns>
                            <DataGridTemplateColumn Header="Test" CellTemplate="{StaticResource template2}" />
                        </DataGrid.Columns>
                    </DataGrid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

其中CustomControl1是

public class CustomControl1 : ItemsControl
{
    static CustomControl1()
    {
        DefaultStyleKeyProperty.OverrideMetadata(
            typeof (CustomControl1),
            new FrameworkPropertyMetadata(typeof (CustomControl1)));
    }
    public static readonly DependencyProperty TestProperty =
        DependencyProperty.Register("Test", typeof (string), typeof (CustomControl1), new PropertyMetadata(default(string)));
    public string Test
    {
        get { return (string) GetValue(TestProperty); }
        set { SetValue(TestProperty, value); }
    }
}

最新更新