2

次のようなリストビューで構成される UserControl があります。

<UserControl
           ....

    <UserControl.Resources>

        <Style TargetType="Thumb">
            <!-- Style Content -->
        </Style>

        <Style  TargetType="GridViewColumnHeader">
            <!-- Style Content -->
        </Style>

        <Style  TargetType="{x:Type ScrollBar}">
            <!-- Style Content -->
        </Style>

        <Style  TargetType="{x:Type ScrollViewer}">
            <!-- Style Content -->
        </Style>

        <Style TargetType="{x:Type ListViewItem}">
            <!-- Style Content -->
        </Style>

    </UserControl.Resources>

    <ListView Name="ListView1" >
            <!-- ListViewContent -->
    </Style>
</UserControl>

私はこれらの userControls を 3 つ持っていますが、それらの間で異なるのは<UserControl.Resources>. 異なるルック アンド フィールが必要という理由だけで、同じ機能を持つ複数のコントロールを作成するシーンはありません。私が今やりたいことは、すべてのスタイル<UserControl.Resources>を 1 つのスタイルに結合することです。これらすべてのスタイルを 1 つにグループ化できれば、3 つのコントロールを削除してスタイルを次のように変更できます。

  <ListView Style={DynamicResource style1} ...

現在、もしそうなら

<UserControl.Resources>
     <Style x:Key="style1">
         <!-- Place all styles in here -->
     </Style>
</UserControl.Resources>

それは動作しません。


編集

iltzortz の回答のおかげで、次のようになりました。

Dictionary1.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style TargetType="Grid">        
        <Setter Property="Background" Value="Green"></Setter>
    </Style>

    <SolidColorBrush x:Key="Foo" Color="Red"></SolidColorBrush>

</ResourceDictionary>

Dictionary2.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style TargetType="Grid">        
        <Setter Property="Background" Value="Black"></Setter>
    </Style>

    <SolidColorBrush x:Key="Foo" Color="Orange"></SolidColorBrush>

</ResourceDictionary>

MyUserControl:

<UserControl x:Class="WpfApplication1.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="97" d:DesignWidth="91">

    <UserControl.Resources>
        <ResourceDictionary Source="Dictionary1.xaml" ></ResourceDictionary>
    </UserControl.Resources>

    <Grid >
        <Ellipse Fill="{DynamicResource Foo}" />
    </Grid>
</UserControl>

そして、次のように動的にリソース ディクショナリを変更します:実行時に wpf リソース ディクショナリを切り替える

4

2 に答える 2

1

たとえば common.xaml という名前のリソース ディクショナリをアプリケーションに追加します。

ここに画像の説明を入力

共通のスタイルをそこに置きます

その後、次の方法で再利用できます。

 <UserControl.Resources>
    <ResourceDictionary Source="common.xaml"/>
 </UserControl.Resources>
于 2013-01-25T15:57:49.800 に答える
0

3 つのリソース ディクショナリを作成し、実行時にマージできます。サンプル コードでは、2 つのリソース ディクショナリを使用しました。

例:

Dictionary1.xaml :

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="btnStyle" TargetType="Button">
        <Setter Property="Margin" Value="0,10,0,0" />
    </Style>
</ResourceDictionary>

Dictionary2.xaml :

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style x:Key="btnStyle" TargetType="Button">
        <Setter Property="Margin" Value="50,50,0,0" />
    </Style>

</ResourceDictionary>

App.xamlアプリケーションの開始時に、ファイルでデフォルトのスタイルを設定できます。

<Application x:Class="Example.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Dictionary1.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

スタイルを変更したい場合は、リソース ディクショナリをマージできます。

ResourceDictionary dict = new ResourceDictionary();
dict.Source = new Uri("\\Dictionary2.xaml", UriKind.Relative);            
this.Resources.MergedDictionaries.Add(dict);

バインディングは次のようになります。

<Button Style="{DynamicResource btnStyle}" Content="Click me!" />

リソース ディクショナリをマージするコードを呼び出すと、ボタンのスタイルが自動的に変更されます。

于 2013-01-25T15:58:36.573 に答える