0

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

显然我根本不了解 TemplateBinding。如何将模板的子属性绑定到要应用模板的 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>
4

1 回答 1

3

您将需要子类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}" />

或者,您可以设置“模板”而不是“内容模板”。这将是当您使用时TemplateBinding

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

通过将属性绑定Items到源项目来使用它:

<local:GridControl Style="{StaticResource HistoryPopupContentStyle}"
                   Items="{Binding Path=HistoryList}" />

您也可以完全跳过创建子类,而只需使用 的Content属性ContentControl来存储项目:

<Style x:Key="HistoryPopupContentStyle" TargetType="ContentControl">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <dxg:GridControl ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=local:GridControl},Path=Content}" />

或者使用 Template / TemplateBinding 方法

<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}" />
于 2014-05-05T20:44:11.657 回答