C# WPF TemplateBinding到模板化父级的DataContext

C# WPF TemplateBinding到模板化父级的DataContext,c#,wpf,xaml,binding,templatebinding,C#,Wpf,Xaml,Binding,Templatebinding,我们有四个相同的弹出窗口,在四个XAML视图中有网格。我想将该XAML移动到一个模板中,并通过一个样式应用到所有四个模板中的ContentControl。问题是传递网格中项目的来源。我们从四种不同的视图模型中分别得到。每种情况都不一样,这四种情况中唯一不同的是。我可能最终会一致地重命名它们,但我想这是另一个问题 显然,我一点也不懂模板绑定。如何将模板子级的属性绑定到应用模板的ContentControl的属性 除了DataSource属性的值发生变化外,网格的XAML与我们直接使用它时非常有效的

我们有四个相同的弹出窗口,在四个XAML视图中有网格。我想将该XAML移动到一个模板中,并通过一个样式应用到所有四个模板中的ContentControl。问题是传递网格中项目的来源。我们从四种不同的视图模型中分别得到。每种情况都不一样,这四种情况中唯一不同的是。我可能最终会一致地重命名它们,但我想这是另一个问题

显然,我一点也不懂模板绑定。如何将模板子级的属性绑定到应用模板的ContentControl的属性

除了DataSource属性的值发生变化外,网格的XAML与我们直接使用它时非常有效的XAML是相同的

我添加了TextBlock,只是想看看是否可以绑定任何东西。我确实在那里得到了一个
NaN

<Style x:Key="HistoryPopupContentStyle" TargetType="ContentControl">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <StackPanel Orientation="Vertical">
                    <TextBlock Text="{TemplateBinding Width, 
                     diag:PresentationTraceSources.TraceLevel=High}"
                               Background="White"
                               Foreground="Black"/>
                <dxg:GridControl
                    DataSource="{Binding RelativeSource={RelativeSource 
                     Path=DataContext, 
                     TraceLevel=High}"
                    VerticalAlignment="Stretch" 
                    HorizontalAlignment="Stretch" 
                    >
                        <!-- Columns. The grid displays column headers 
                                 as desired but with no rows -->
                    </dxg:GridControl.Columns>
                </dxg:GridControl>
                </StackPanel>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Popup 
    Name="PopHistory" 
    DataContext="{Binding Path=HistoryList}"
    >
    <ContentControl DataContext="{Binding Path=HistoryList}"
                    Style="{StaticResource HistoryPopupContentStyle}"
                    Name="Testing"
                    />
</Popup>

您将需要对
ContentControl
子类化(或者只对
Control
),以便添加新的依赖项属性

public class GridControl : ContentControl
{
    // TODO add dependency properties

    public GridControl()
    {
        DefaultStyleKey = typeof(GridControl);
    }
}
将“Items”依赖项属性添加到上述控件(键入
IEnumerable

接下来,更新模板以针对新类型:

<Style x:Key="HistoryPopupContentStyle" TargetType="local:GridControl">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <dxg:GridControl ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=local:GridControl},Path=Items}" />
通过将
属性绑定到源项来使用它:

<local:GridControl Style="{StaticResource HistoryPopupContentStyle}"
                   Items="{Binding Path=HistoryList}" />
或者使用模板/模板绑定方法

<Style x:Key="HistoryPopupContentStyle" TargetType="ContentControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ContentControl">
                <dxg:GridControl ItemsSource="{TemplateBinding Content}" />

这样使用:

<ContentControl Style="{StaticResource HistoryPopupContentStyle}"
                Content="{Binding Path=HistoryList}" />

您将需要对
ContentControl
子类化(或者只对
Control
),以便添加新的依赖项属性

public class GridControl : ContentControl
{
    // TODO add dependency properties

    public GridControl()
    {
        DefaultStyleKey = typeof(GridControl);
    }
}
将“Items”依赖项属性添加到上述控件(键入
IEnumerable

接下来,更新模板以针对新类型:

<Style x:Key="HistoryPopupContentStyle" TargetType="local:GridControl">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <dxg:GridControl ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=local:GridControl},Path=Items}" />
通过将
属性绑定到源项来使用它:

<local:GridControl Style="{StaticResource HistoryPopupContentStyle}"
                   Items="{Binding Path=HistoryList}" />
或者使用模板/模板绑定方法

<Style x:Key="HistoryPopupContentStyle" TargetType="ContentControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ContentControl">
                <dxg:GridControl ItemsSource="{TemplateBinding Content}" />

这样使用:

<ContentControl Style="{StaticResource HistoryPopupContentStyle}"
                Content="{Binding Path=HistoryList}" />

您将需要对
ContentControl
子类化(或者只对
Control
),以便添加新的依赖项属性

public class GridControl : ContentControl
{
    // TODO add dependency properties

    public GridControl()
    {
        DefaultStyleKey = typeof(GridControl);
    }
}
将“Items”依赖项属性添加到上述控件(键入
IEnumerable

接下来,更新模板以针对新类型:

<Style x:Key="HistoryPopupContentStyle" TargetType="local:GridControl">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <dxg:GridControl ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=local:GridControl},Path=Items}" />
通过将
属性绑定到源项来使用它:

<local:GridControl Style="{StaticResource HistoryPopupContentStyle}"
                   Items="{Binding Path=HistoryList}" />
或者使用模板/模板绑定方法

<Style x:Key="HistoryPopupContentStyle" TargetType="ContentControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ContentControl">
                <dxg:GridControl ItemsSource="{TemplateBinding Content}" />

这样使用:

<ContentControl Style="{StaticResource HistoryPopupContentStyle}"
                Content="{Binding Path=HistoryList}" />

您将需要对
ContentControl
子类化(或者只对
Control
),以便添加新的依赖项属性

public class GridControl : ContentControl
{
    // TODO add dependency properties

    public GridControl()
    {
        DefaultStyleKey = typeof(GridControl);
    }
}
将“Items”依赖项属性添加到上述控件(键入
IEnumerable

接下来,更新模板以针对新类型:

<Style x:Key="HistoryPopupContentStyle" TargetType="local:GridControl">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <dxg:GridControl ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=local:GridControl},Path=Items}" />
通过将
属性绑定到源项来使用它:

<local:GridControl Style="{StaticResource HistoryPopupContentStyle}"
                   Items="{Binding Path=HistoryList}" />
或者使用模板/模板绑定方法

<Style x:Key="HistoryPopupContentStyle" TargetType="ContentControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ContentControl">
                <dxg:GridControl ItemsSource="{TemplateBinding Content}" />

这样使用:

<ContentControl Style="{StaticResource HistoryPopupContentStyle}"
                Content="{Binding Path=HistoryList}" />


Ohhh,内容!我从没想过。谢谢,我来试一试。哦,满意了!我从没想过。谢谢,我来试一试。哦,满意了!我从没想过。谢谢,我来试一试。哦,满意了!我从没想过。谢谢,我来试一试。