开发者

Can every template be put in the window.resources section?

开发者 https://www.devze.com 2023-02-20 20:58 出处:网络
I was wondering something: if you c开发者_Python百科reate a template under a control (like datagrid), can you always put everything you make under the window.resources? Will give an example:

I was wondering something: if you c开发者_Python百科reate a template under a control (like datagrid), can you always put everything you make under the window.resources? Will give an example:

<Grid Background="DarkGray">
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="20" />
    </Grid.RowDefinitions>
    <DataGrid Name="dgFruit" ItemsSource="{Binding}" AutoGenerateColumns="false" ItemTemplate="{StaticResource datagrid}" >
        <DataGrid.RowHeaderStyle>
            <Style TargetType="DataGridRowHeader">
                <Setter Property="Content" Value="{Binding Path=Number}" />
            </Style>
        </DataGrid.RowHeaderStyle>
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Path=Name}" Header="Name"/>
            <DataGridTextColumn Binding="{Binding Path=Color}" Header="Color"/>
            <DataGridCheckBoxColumn Binding="{Binding Path=Mjummy}" Header="Mjummy"/>
            <DataGridTextColumn Binding="{Binding Path=Number}" Header="Number"/>
            <DataGridTextColumn Binding="{Binding Path=Pits.Count}" Header="Pits"/>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

Can you make a datatemplate of everything from <DataGrid.RowHeaderStyle> to </DataGrid.Columns> ? And if so, could you tell how?

Thanks in advance


That's not something i'd put in a template, but rather in a style, and yes, this style can be put in the resources.


Yes.

It's really important to know how WPF finds resources. When it's trying to find a resource, it first looks in the current object's resource dictionary. If the resource isn't found, then it looks in its container's resource dictionary. It does this recursively, until it reaches an object that has no container (i.e. is a Window), in which case it looks in the application's resource dictionary, and then, finally, give up and tell you that the resource can't be found.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号