1

基本スタイルのプロパティをオーバーライドしようとしていますが、実行時に。たとえば、ユーザーがフォントサイズやフォントファミリーなどを変更できるようにする設定ページがあります。これらはすべて共通のプロパティです。したがって、これらすべての基本的なプロパティを定義した構造があります。フォント サイズを 11px から 14px に変更すると、アプリケーション内のすべての要素がこの変更を継承するはずです。

問題は、すべてのプロパティを格納する基本スタイルを変更できないことです。

以下のコードは私の基本スタイルを示しています:

<Style x:Key="BaseStyle">
        <Setter Property="Control.FontFamily" Value="Arial"></Setter>
        <Setter Property="Control.FontSize" Value="11px"/>
        <Setter Property="Control.Foreground" Value="Red"/>
</Style>

これで、この基本スタイルから継承する別のスタイルができました。

 <Style TargetType="TextBox" BasedOn="{StaticResource BaseStyle}">
       <Setter Property="Control.Background" Value="{DynamicResource NormalBrush}"/>
 </Style>

アプリケーションには、フォント サイズを変更するためのコンボ ボックスがあります。

<ComboBox Height="23" HorizontalAlignment="Left" Name="comboBox2" SelectedValue="FontSizeValue" Style="{x:Null}" Width="92">
                        <ComboBoxItem Content="12px"/>
                        <ComboBoxItem Content="13px"/>
                        <ComboBoxItem Content="14px"/>
                        <ComboBoxItem Content="15px"/>
</ComboBox>

アプリケーションでこのコンボ ボックスから値を選択すると、ベース スタイルを更新する必要があります。私にはできないことです。これを達成する方法についての提案。すべてのプロパティの変更は動的に発生するはずです。

4

1 に答える 1

1

基本的なスタイルは、このタイプのコントロールでは変更されない値にする必要があります。変更が必要な値は、ベースを継承できる別のスタイルで指定されます。例:

<Window.Resources>
    <!-- Main style for all controls -->
    <Style x:Key="BaseStyle" TargetType="{x:Type Control}">
        <Setter Property="FontFamily" Value="Arial" />
        <Setter Property="FontSize" Value="11px" />
        <Setter Property="Foreground" Value="Black" />
        <Setter Property="Width" Value="200" />
        <Setter Property="Height" Value="25" />
    </Style>

    <!-- This style inherits all the settings from the base style, but set the background -->
    <Style x:Key="DefaultBaseStyle" BasedOn="{StaticResource BaseStyle}" TargetType="{x:Type TextBox}">
        <Setter Property="Background" Value="Green" />
    </Style>

    <!-- This style inherits only the width and height -->
    <Style x:Key="NotDefaultBaseStyle" BasedOn="{StaticResource BaseStyle}" TargetType="{x:Type TextBox}">
        <Setter Property="Background" Value="Black" />
        <Setter Property="Foreground" Value="White" />
        <Setter Property="FontFamily" Value="Courier New" />
    </Style>
</Window.Resources>

<Grid>
    <StackPanel>
        <TextBox Style="{StaticResource DefaultBaseStyle}" Text="Default base style" Margin="0,10,0,0" />
        <TextBox Style="{StaticResource NotDefaultBaseStyle}" Text="Not default base style" Margin="0,10,0,0" />
    </StackPanel>
</Grid>

Output

ここに画像の説明を入力

さまざまな種類のコントロールが多数ある場合は、共通のもの (幅、高さ、配置など) を選択して、それぞれに基本的なスタイルを作成することをお勧めします。たとえば、 、 などのベース スタイルButton。ベースTextBoxとは大きく異なるものを制御するため、ベースを継承する別のスタイルを作成する必要があります。

EDIT:

ユーザーの選択に応じてスタイルを変更する場合は、これらのパラメーターを使用して設定を作成する必要があります。それで、プロジェクトの設定に入ります:

Project -> Properties -> Parameters

name MyColor、文字列の型で設定を作成します。設定のスタイルに関連付けるには、次のように記述する必要があります。

xmlns:properties="clr-namespace:DynamicStyleHelp.Properties"

<Setter Property="Background" Value="{Binding Source={x:Static properties:Settings.Default}, Path=MyColor, Mode=TwoWay}" />

これで、setter は設定の値を参照します。コードの背後にあるプロパティを変更します。

// your namespace.Properties.Settings.Default.your name of property
DynamicStyleHelp.Properties.Settings.Default.MyColor = "Red";   

以下は完全な例です。

XAML

<Window x:Class="DynamicStyleHelp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:properties="clr-namespace:DynamicStyleHelp.Properties"
    Title="MainWindow" Height="350" Width="525"
    WindowStartupLocation="CenterScreen">

<Window.Resources>
    <Style x:Key="BaseStyle" TargetType="{x:Type Control}">
        <Setter Property="FontFamily" Value="Arial" />
        <Setter Property="FontSize" Value="11px" />
        <Setter Property="Background" Value="{Binding Source={x:Static properties:Settings.Default}, Path=MyColor, Mode=TwoWay}" />
        <Setter Property="Width" Value="200" />
        <Setter Property="Height" Value="25" />
    </Style>

    <Style x:Key="DefaultBaseStyle" BasedOn="{StaticResource BaseStyle}" TargetType="{x:Type TextBox}">
        <Setter Property="Foreground" Value="Black" />
    </Style>

    <Style x:Key="NotDefaultBaseStyle" BasedOn="{StaticResource BaseStyle}" TargetType="{x:Type TextBox}">
        <Setter Property="Foreground" Value="White" />
        <Setter Property="FontFamily" Value="Courier New" />
    </Style>
</Window.Resources>

<Grid>
    <StackPanel>
        <TextBox Style="{StaticResource DefaultBaseStyle}" Text="Default base style" Margin="0,10,0,0" />
        <TextBox Style="{StaticResource NotDefaultBaseStyle}" Text="Not default base style" Margin="0,10,0,0" />

        <Button Name="ChangeButton" Width="100" Height="30" Content="ChangeButton" Margin="0,10,0,0" Click="ChangeButton_Click" />
    </StackPanel>
</Grid>
</Window>

Code behind

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void ChangeButton_Click(object sender, RoutedEventArgs e)
    {
        DynamicStyleHelp.Properties.Settings.Default.MyColor = "Red";
    }
}
于 2013-08-19T05:43:02.403 に答える