Edw*_*uay 16 wpf resources xaml
在即使是最小的WPF例子和原型我一直在做,在<Windows.Resources>开始臃肿快.把它重新放入app.xaml我的Windows和UserControls,但它很难组织(Visual Studio"XAML折叠"功能没有任何帮助,因为你只有一个页面充满了"Style ..."这个词).
另外,我正在努力想出一种易于记忆和有组织的方式来命名我的风格.最好的方法我发现它只是为了冗长和描述性,所以我得到这样的东西:BottomMainLeftScrollViewerStyle等.但这有其局限性,很快也会让人感到困惑.我决定使用camelCase作为样式名称,以便在XAML的页面和页面中轻松发现它们.
您有什么策略可以防止WPF资源变得笨拙?
<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>
Run Code Online (Sandbox Code Playgroud)
Ken*_*art 15
使用单独的ResourceDictionarys并根据需要将它们合并到可视树中的适当级别.
<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>
Run Code Online (Sandbox Code Playgroud)