1

I need to reuse an existing data template, defined as a reource, in a data template defined in a control

...this is my customer data template as a resource so it can be a reusable object

Customer DataTemplate:

<ResourceDictionary ... >

    <DataTemplate DataType="{x:Type Model:Customer}">

        <StackPanel Orientation="Vertical" Width="Auto">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition Width="140" />
                    </Grid.ColumnDefinitions>                   

                    <Grid.RowDefinitions>
                        <RowDefinition Height="20"/>
                        <RowDefinition Height="25"/>
                    </Grid.RowDefinitions>

                    <TextBox Text="{Binding CustomerName}"
                        Grid.Column="0" Grid.Row="0" />
        </StackPanel>

    </DataTemplate>

</ResourceDictionary>

now I need to reuse the Customer data template defined above in a user control data template

<UserControl x:Class="MyCustomerControl"

    <UserControl.Resources >

        <DataTemplate DataType="{x:Type frnri:CustomerViewModel}">

            question: How can I reuse customer data template here?

        </DataTemplate>


    </UserControl.Resources>

</UserControl>

Is possibile this scenario WITHOUT defined the customer data template inline in the MyCustomerControl?

4

1 回答 1

0

Place your datatemplate in App.xaml, then it will be available throughout your program.

Declare the resource as follows

<Application.Resources>
<DataTemplate DataType="{x:Type Model:Customer}" x:Key="customerTemplate">

    <StackPanel Orientation="Vertical" Width="Auto">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition Width="140" />
                </Grid.ColumnDefinitions>                   

                <Grid.RowDefinitions>
                    <RowDefinition Height="20"/>
                    <RowDefinition Height="25"/>
                </Grid.RowDefinitions>

                <TextBox Text="{Binding CustomerName}"
                    Grid.Column="0" Grid.Row="0" />
    </StackPanel>

</DataTemplate>
</Application.Resources>

Then you can reference it in your UserControl like

<ListBox ItemTemplate="{StaticResource customerTemplate}" />

The same will work for the ResouceDictionary

于 2012-07-01T16:51:45.663 回答