1

他のいくつかの基本的なコントロールを内部に含むカスタム コントロールを WPF で開発しているとします。簡単にするために、2 つのボタンが含まれているとします

ここで、このカスタム コントロールをアプリで使用したいと考えていますが、スタイルを少し変更したいと考えてます。

ケース1

カスタム コントロールの定義で、両方のボタンのスタイルが同じ (wpf のデフォルト) で、両方のスタイルを変更したい場合は、簡単です。

<mc:MyControl>
   <mc:MyControl.Resources>
      <Style x:Key={x:Type Button}, TargetType={x:Type Button}>
         <!-- Insert the new style here -->
      </Style>
   </mc:MyControl.Resources>
<mc:MyControl>

ケース 2

カスタム コントロールの定義で、両方のボタンが同じスタイル (wpf デフォルト) を持っているが、2 つの異なるスタイルでスタイルを変更したい場合、それを解決する最善の方法は何ですか?

ケース 3

カスタム コントロールの定義で、両方のボタンに同じスタイルがあり、それがカスタム コントロール内で定義された Style を参照していて、スタイルを変更したい場合、それを解決する最善の方法は何ですか?

すべての助けを前もってありがとう

4

1 に答える 1

5

カスタムコントロールで2つの異なるStyleプロパティを定義し、それらを個々のボタンのStyleプロパティにバインドして、クライアントが2つのコントロールのスタイルを互いに独立して設定できるようにすることができます。

XAMLファイル:

<UserControl x:Class="MyNamespace.MyControl"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Name="MyControl">
  <StackPanel>
    <Button Name="FirstButton"
            Style={Binding ElementName=MyControl, Path=FirstButtonStyle}
            Content="First Button" />
    <Button Name="SecondButton"
            Style={Binding ElementName=MyControl, Path=SecondButtonStyle}
            Content="Second Button" />
  </StackPanel>
</UserControl>

コードビハインドファイル:

using System;
using System.Windows;
using System.Windows.Controls;

namespace MyNamespace
{
  [StyleTypedProperty(
  Property = "FirstButtonStyle",
  StyleTargetType = typeof(Button))]
  [StyleTypedProperty(
  Property = "SecondButtonStyle",
  StyleTargetType = typeof(Button))]
  public partial class MyControl : UserControl
  {
    public static readonly DependencyProperty FirstButtonStyleProperty =
      DependencyProperty.Register(
        "FirstButtonStyle",
        typeof(Style),
        typeof(MyControl)
      );

    public Style FirstButtonStyle
    {          
      get { return (Style)GetValue(FirstButtonStyleProperty); }
      set { SetValue(FirstButtonStyleProperty, value); }
    }


    public static readonly DependencyProperty SecondButtonStyleProperty =
      DependencyProperty.Register(
        "SecondButtonStyle",
        typeof(Style),
        typeof(MyControl)
      );

    public Style SecondButtonStyle
    {          
      get { return (Style)GetValue(SecondButtonStyleProperty); }
      set { SetValue(SecondButtonStyleProperty, value); }
    }
  }
}

これらの2つのプロパティを依存関係プロパティとして実装すると、標準のWPFコントロールの他のスタイルプロパティに準拠するようになるため、これらのプロパティを実装することをお勧めします。

これで、他のWPFコントロールと同じようにボタンのスタイルを設定できます。

<Window 
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:local="clr-namespace:MyNamespace"
  Title="MyControl Sample" 
  Height="300" 
  Width="300">
  <Window.Resources>
    <Style x:Key="GreenButton" TargetType="{x:Type Button}">
      <Setter Property="Background" Value="Green" />
    </Style>
    <Style x:Key="RedButton" TargetType="{x:Type Button}">
      <Setter Property="Background" Value="Red" />
    </Style>
  </Windows.Resources>
  <StackPanel>
    <local:MyControl FirstButtonStyle="{StaticResource GreenButton}"
                     SecondButtonStyle="{StaticResource RedButton}" />
  </StackPanel>
</Window>

または、両方の内部コントロールのStyleプロパティにバインドするカスタムコントロールの単一のプロパティを公開することにより、2つのボタンで同じスタイルを共有することもできます。

アップデート

FirstButtonStyleプロパティとSecondButtonStyleプロパティを依存関係プロパティとして定義する必要はありません。重要なことは、ボタンのスタイルプロパティへの内部バインディングは、値が変更されるたびに更新されることです。これを実現するには、ユーザーコントロールにINotifyPropertyChangedインターフェイスを実装し、プロパティセッターでOnPropertyChangedイベントを発生させます。

ユーザーコントロールのコンストラクターの2つのプロパティに「デフォルトスタイル」を割り当てることもできます。次に例を示します。

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;

namespace MyNamespace
{
  [StyleTypedProperty(
  Property = "FirstButtonStyle",
  StyleTargetType = typeof(Button))]
  [StyleTypedProperty(
  Property = "SecondButtonStyle",
  StyleTargetType = typeof(Button))]
  public partial class MyControl : UserControl, INotifyPropertyChanged
  {
    private Style firstButtonStyle;
    private Style secondButtonStyle;

    public MyControl()
    {
      Style defaultStyle = new Style();
      // assign property setters to customize the style

      this.FirstButtonStyle = defaultStyle;
      this.SecondButtonStyle = defaultStyle; 
    }

    public Style FirstButtonStyle
    {          
      get { return firstButtonStyle; }
      set
      {
         firstButtonStyle = value;
         OnPropertyChanged("FirstButtonStyle");
      }
    }

    public Style SecondButtonStyle
    {          
      get { return secondButtonStyle; }
      set
      {
         secondButtonStyle = value;
         OnPropertyChanged("SecondButtonStyle");
      }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
      if (PropertyChanged != null)
      {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
      }
    }
  }
}
于 2009-03-02T23:29:11.130 に答える