How are you keeping your WPF resources organized on large projects?

How are you keeping your WPF resources organized on large projects?

囚你心 发布于 2021-11-26 字数 3731 浏览 734 回复 1 原文

On even the smallest WPF examples and prototypes I've been doing, the <Windows.Resources> begins to bloat fast. Putting it back into app.xaml puts it all out of my Windows and UserControls but it is hard to organize (the Visual Studio "XAML folding" feature is of no help since you just have a page full of the word "Style...").

In addition, I'm struggling to come upon an easy-to-remember and organized way of naming my styles. The best way I have found it just to be long and descriptive, so I get things like this: BottomMainLeftScrollViewerStyle, etc. But this has its limits and soon gets confusing as well. I decided to use camelCase for style names to easily spot them in pages and pages of XAML.

What are your strategies for preventing WPF resources from becoming unwieldy?

<Window.Resources>

    <local:CutOffConverter x:Key="AgeConverter" Cutoff="30"/>

    <Style TargetType="Grid" x:Key="customerGridMainStyle">
        <Setter Property="Background">
            <Setter.Value>
                <LinearGradientBrush StartPoint="0,0" EndPoint=".5,.5">
                    <GradientStop Offset="0.0" Color="#888"/>
                    <GradientStop Offset="1.0" Color="#ccc"/>
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
    </Style>

    <Style TargetType="StackPanel" x:Key="mainStackPanelStyle">
        <Setter Property="HorizontalAlignment" Value="Left"/>
    </Style>
    <Style TargetType="ScrollViewer" x:Key="mainScrollViewerStyle">
        <Setter Property="Height" Value="250"/>
    </Style>
    <Style TargetType="ListBox" x:Key="mainListBoxStyle">
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="Margin" Value="10"/>
    </Style>


    <ObjectDataProvider x:Key="customers"
                        ObjectType="{x:Type local:Customer}"
                        MethodName="GetAllCustomers"/>

    <DataTemplate DataType="{x:Type local:Customer}">
        <Grid x:Name="MainGrid" Style="{StaticResource customerGridMainStyle}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="100"/>
                <ColumnDefinition Width="150"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <TextBlock Grid.Column="0" Grid.Row="0" Text="First Name" Margin="5"/>
            <TextBlock Grid.Column="1" Grid.Row="0" Text="{Binding FirstName}" Margin="5"/>
            <TextBlock Grid.Column="0" Grid.Row="1" Text="Last Name" Margin="5"/>
            <TextBlock Grid.Column="1" Grid.Row="1" Text="{Binding LastName}" Margin="5"/>
            <TextBlock Grid.Column="0" Grid.Row="2" Text="Age" Margin="5"/>
            <TextBlock x:Name="Age" Grid.Column="1" Grid.Row="2" Text="{Binding Age}" Margin="5"/>
        </Grid>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding Path=Age, Converter={StaticResource AgeConverter}}">
                <DataTrigger.Value>true</DataTrigger.Value>
                <Setter TargetName="Age" Property="Foreground" Value="Red"/> 
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>
</Window.Resources>

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。

扫码加入群聊

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

守望孤独 2022-06-07 1 楼

Use separate ResourceDictionarys and merge them into the appropriate levels in your visual tree as needed.

<App ...>
    <App.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="ListBoxResources.xaml"/>
                <ResourceDictionary Source="ComboBoxResources.xaml"/>
                <ResourceDictionary Source="LabelResources.xaml"/>
                <ResourceDictionary Source="TextBoxResources.xaml"/>
            </ResourceDictionary.MergedDictionaries>
            <!-- if you have local resources, place them here.
                (as noted by Mark Synowiec in the comments)
             -->
        </ResourceDictionary>
    </App.Resources>
</App>