简体   繁体   中英

Applying default style in templated control from Generic.xaml

I have created a templated control in WinRT/UWP with a GridView inside of it. I created an ItemTemplate dependency property that will house the DataTemplate of the grid view's items, so that when the control is re-used on other pages, I have the flexibility to change the item templates in each implementation.

However, with the current code that I have, it loads the control properly but the styling is not implemented.

Here's my Generic.xaml code:

<Style TargetType="ctrl:CustomGridView">
    <Setter Property="ItemTemplate">
        <Setter.Value>
            <DataTemplate>
                <Grid>
                    <Grid Background="Red">
                            <TextBlock Text="{Binding Name, Mode=OneWay}" Foreground="White" TextAlignment="Center"/>
                    </Grid>
                </Grid>
            </DataTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ctrl:CustomGridView">
                <Grid>
                    <ContentPresenter x:Name="templateProxy" Width="{TemplateBinding ItemWidth}" Height="{TemplateBinding ItemHeight}"  ContentTemplate="{TemplateBinding ItemTemplate}" Visibility="Collapsed" />
                    <GridView x:Name="gridView" ItemsSource="{TemplateBinding ItemsSource}"
                              SelectionMode="None" IsItemClickEnabled="True" IsSwipeEnabled="False" IsTabStop="False">
                        <GridView.ItemTemplate>
                            <DataTemplate>
                                <ContentPresenter Width="{Binding Width, ElementName=templateProxy}" Height="{Binding Height, ElementName=templateProxy}" ContentTemplate="{TemplateBinding ItemTemplate}" />
                            </DataTemplate>
                        </GridView.ItemTemplate>
                    </GridView>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

The goal is to make the ItemTemplate set in Generic.xaml to be the default one, and allow to change it on different implementations of the control.

Here's how the ItemTemplate property is defined in the CustomGridView.cs file:

    public static readonly DependencyProperty ItemTemplateProperty =
       DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(PatientGridView), new PropertyMetadata(null));

    public DataTemplate ItemTemplate
    {
        get { return (DataTemplate)GetValue(ItemTemplateProperty); }
        set { SetValue(ItemTemplateProperty, value); }
    }

Is there anything I should do in the CustomGridView.cs to load the default ItemTemplate? Am I missing anything?

I'll provide additional code if necessary.

Thank you very much!

You have to include this code:

[assembly: ThemeInfo(ResourceDictionaryLocation.SourceAssembly, ResourceDictionaryLocation.SourceAssembly)]

to your AssemblyInfo.cs file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM