0

4 つの XAML ビューにグリッドを含む 4 つの同一のポップアップがあります。その XAML をテンプレートに移動し、スタイルを介して 4 つすべての ContentControls に適用したいと思います。問題は、グリッド内のアイテムのソースを渡すことです。これは、4 つの異なるビュー モデルのそれぞれから得られます。4 つのケースで異なるのは、ケースごとに異なります。おそらく一貫して名前を変更することになりますが、それは別の問題だと思います。

明らかに、私は TemplateBinding をまったく理解していません。テンプレートの子のプロパティを、テンプレートを適用する ContentControl のプロパティにバインドするにはどうすればよいですか?

DataSource 属性の値が変わることを除けば、グリッドの 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}" />

または、「ContentTemplate」の代わりに「Template」を設定することもできます。これは、次を使用する場合です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 に答える