2

特定のプロパティ (およびイベント) を持つカスタム WPF コントロールを作成しています。My Button は、現在設定されている Button の背景色に従って前景色を変更できる必要があります。

これは、オーバーライドされた OnApplyTemplate 関数で色を変更するときに機能しますが、これを動的に行う方法がわかりませんでした (コントロールが読み込まれた後)。

Controls Background プロパティに DependencyPropertyChanged イベント ハンドラーを追加できれば、これで問題は解決しますが、その方法がわかりません。

うまくいく他のかなり醜い解決策:

  • 現在の背景色をチェックするボタンごとに個別のバックグラウンド ワーカー (十分な数のボタンが存在する場合、実際のパフォーマンス キラーになる可能性があります)
  • Background の代わりにカスタムの BackgroundColor プロパティを使用します (機能しますが、あまりエレガントには見えません)。

誰かが解決策を持っていますか?

編集:

OK、コントロールの Background プロパティがその親の Background プロパティに従って変化する可能性があることを確認した後 (特に設定されていない場合)、実際には Button の背景にのみ影響する別の BackgroundColor プロパティを追加します。

4

1 に答える 1

3

バインディングコンバーターでこれを行うことができます。

http://tech.pro/tutorial/806/wpf-tutorial-binding-converters

または、おそらく実装が簡単なトリガーです。

http://wpftutorial.net/Triggers.html

編集(いくつかの例):

バインディング コンバーターのサンプル - UserControl のみですが、その方法を示す必要があります。

UCButton.xaml.cs:

using System;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;

namespace Test3
{
    public partial class UCButton : UserControl
    {
        public UCButton()
        {
            InitializeComponent();

            this.DataContext = this;
        }
    }

    [ValueConversion(typeof(Brush), typeof(Brush))]
    public class BrushConverter : IValueConverter
    {
        public object Convert(object value, Type targetType,
               object parameter, System.Globalization.CultureInfo culture)
        {
            Brush background = (Brush)value;

            if (background == Brushes.Pink)
                return Brushes.Red;
            else if (background == Brushes.LightBlue)
                return Brushes.DarkBlue;
            else if (background == Brushes.LightGreen)
                return Brushes.DarkGreen;
            else
                return Brushes.Black;
        }

        public object ConvertBack(object value, Type targetType,
               object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

UCButton.xaml

<UserControl x:Class="Test3.UCButton"
             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" 
             xmlns:local="clr-namespace:Test3" mc:Ignorable="d" 
             d:DesignHeight="29" d:DesignWidth="82">

    <UserControl.Resources>
        <local:BrushConverter x:Key="brushConverter" />
    </UserControl.Resources>

    <Grid>
        <Button Content="Button" Name="button1" Background="{Binding Background}" Foreground="{Binding Background, Converter={StaticResource brushConverter}}" />
    </Grid>
</UserControl>

トリガーの使用例:

MainWindow.xaml に次を追加します。

<Window.Resources>
    <Style x:Key="buttonBrushStyle" TargetType="Button">
        <Style.Triggers>
            <Trigger Property="Background" Value="Pink">
                <Setter Property="Foreground" Value="Red" />
            </Trigger>
            <Trigger Property="Background" Value="LightBlue">
                <Setter Property="Foreground" Value="DarkBlue" />
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

<Button Style="{StaticResource buttonBrushStyle}" Content="Button"
   Height="23" Width="75" Background="Pink" />
<Button Style="{StaticResource buttonBrushStyle}" Content="Button"
   Height="23" Width="75" Background="LightBlue" />
于 2013-03-12T09:51:24.100 に答える