2011-10-17 7 views
3

ButtonとComboBoxを含むWPF UserControlを作成しました。私はマウスの位置に応じて、両方のスタイルを変更したいので、マウスの上のUIElementは黒色になり、もう一方は赤色になります。いずれもスタイルが設定されていない場合、デフォルトのスタイルが適用されます。WPF UserControls;トリガーと他のコントロールの変更

この悪夢の配色は心配する必要はありません。

ご協力いただきありがとうございます。

XAML

<UserControl x:Class="WpfUserControlSample.ToolbarButtonCombo" 
      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:WpfUserControlSample" 
      x:Name="Control" 
      mc:Ignorable="d" 
      d:DesignHeight="30">  
    <UserControl.Resources> 
     <Style TargetType="{x:Type local:ToolbarButtonCombo}"> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding IsButtonMouseOver}" Value="True"> 
        <Setter Property="ButtonStyle" Value="Black"/> 
        <Setter Property="ComboStyle" Value="Red"/>      
       </DataTrigger> 
       <!-- 
       <DataTrigger Binding="{Binding IsComboMouseOver}" Value="True"> 
        <Setter Property="ButtonStyle" Value="Red"/> 
        <Setter Property="ComboStyle" Value="Black"/> 
       </DataTrigger> 
       --> 
      </Style.Triggers> 
     </Style> 
    </UserControl.Resources> 
    <StackPanel Orientation="Horizontal" Height="30"> 
     <Button Name="btn" Background="{Binding ButtonStyle,ElementName=Control,Mode=OneWay}"> 
      Test 
     </Button> 
     <ComboBox Name="cmb" Background="{Binding ComboStyle,ElementName=Control,Mode=OneWay}"></ComboBox> 
    </StackPanel> 
</UserControl> 

分離コード

namespace WpfUserControlSample 
{ 
    public partial class ToolbarButtonCombo : UserControl, INotifyPropertyChanged 
    { 
     public event PropertyChangedEventHandler PropertyChanged; 
     protected void OnPropertyChanged(string propertyName) 
     { 
      if (PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 

     } 
     public ToolbarButtonCombo() 
     { 
      InitializeComponent(); 
      btn.MouseEnter += new MouseEventHandler(btn_MouseChanged); 
      btn.MouseLeave += new MouseEventHandler(btn_MouseChanged); 
     } 
     void btn_MouseChanged(object sender, MouseEventArgs e) 
     { 
      OnPropertyChanged("IsButtonMouseOver"); 
     } 


     public bool IsButtonMouseOver 
     { 
      get { return btn.IsMouseOver; } 

     } 
     public static readonly DependencyProperty IsButtonMouseOverProperty = 
      DependencyProperty.Register("IsButtonMouseOver", typeof(string), typeof(ToolbarButtonCombo), new PropertyMetadata("false")); 

     public string ButtonStyle { get; set; } 
     public static readonly DependencyProperty ButtonStyleProperty = 
      DependencyProperty.Register("ButtonStyle", typeof(string), typeof(ToolbarButtonCombo)); 

     public string ComboStyle { get; set; } 
     public static readonly DependencyProperty ComboStyleProperty = 
      DependencyProperty.Register("ComboStyle", typeof(string), typeof(ToolbarButtonCombo));  
    } 
} 
+1

IsButtonMouseOverにブレークポイントを設定するか、返される前にConsole.WriteLineを追加すると、これは決して呼び出されませんが、私はあまり経験はありませんが、この部分は私にとっては奇妙に思えます。実際のIsMouseOverに転送します。個人的には、私ができるなら、通常のTriggerをIsMouseOverプロパティで直接使用しますが、私は初心者で、既存のコードを変更する方法は見当たりません。がんばろう。 – Pierre

答えて

2

二つの問題があります。

まず、DataTriggerのバインディングが正しく表示されません。彼らは、関連付けられたコントロールではなく、DataContext上のIsButtonMouseOverを探しています。あなたは使用する必要があるだろう:

<DataTrigger Binding="{Binding IsButtonMouseOver, RelativeSource={RelativeSource Self}}" Value="True"> 
    <Setter Property="ButtonStyle" Value="Black"/> 
    <Setter Property="ComboStyle" Value="Red"/>      
</DataTrigger> 

または:

<Trigger Property="IsButtonMouseOver" Value="True"> 
    <Setter Property="ButtonStyle" Value="Black"/> 
    <Setter Property="ComboStyle" Value="Red"/>      
</Trigger> 

他があなたのIsButtonMouseOverが正しく実装されていないです。 、さらに正確

public static readonly DependencyProperty IsButtonMouseOverProperty = DependencyProperty.Register("IsButtonMouseOver", 
    typeof(bool), typeof(ToolbarButtonCombo), new PropertyMetadata(false)); 

    public bool IsButtonMouseOver 
    { 
     get { return (bool)this.GetValue(IsButtonMouseOverProperty); } 
     set { this.SetValue(IsButtonMouseOverProperty, value); } 
    } 

    void btn_MouseChanged(object sender, MouseEventArgs e) 
    { 
     this.IsButtonMouseOver = this.btn.IsMouseOver; 
    } 

それともIsButtonMouseOver読み取り専用そうのような依存関係プロパティます:あなたのような何かを行う必要があり、適切である必要がある

private static readonly DependencyPropertyKey IsButtonMouseOverPropertyKey = DependencyProperty.RegisterReadOnly("IsButtonMouseOver", 
    typeof(bool), typeof(ToolbarButtonCombo), new FrameworkPropertyMetadata(false)); 

public static readonly DependencyProperty IsButtonMouseOverProperty = ToolbarButtonCombo.IsButtonMouseOverPropertyKey.DependencyProperty; 

public bool IsButtonMouseOver { 
    get { return (bool)this.GetValue(IsButtonMouseOverProperty); } 
    private set { this.SetValue(IsButtonMouseOverPropertyKey, value); } 
} 

あなたの他のプロパティ(のButtonStyleとComboStyle)また、get/setメソッドは依存関係プロパティによってサポートされていません。

+0

それはすごくうまくいきます、そして、私は今どこで間違っていたか見ることができます。どうもありがとう! –

関連する問題