2012-03-26 20 views
0

Ich habe ein UserControl für die Verwendung in meiner App erstellt. Es besteht aus einer ComboBox in einem Grid mit zwei RepeatButtons. Dieses Steuerelement wird in einer Anwendung verwendet, die auf einem mit einem Touchscreen ausgestatteten Laptop ausgeführt wird. Die Schaltflächen werden verwendet, um die nächste oder vorherige Auswahl in der ComboBox auszuwählen. Hier ist die XAML für die Steuerung:Wie kann ich diesen Auslöser arbeiten lassen?

<UserControl x:Class="CarSystem.CustomControls.TouchComboBox" 
      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:cs="clr-namespace:CarSystem.CustomControls" 
      mc:Ignorable="d" 
      Focusable="True" 
      GotFocus="UserControl_GotFocus"> 

    <UserControl.Resources> 
     <Style x:Key="FocusedStyle" TargetType="{x:Type ComboBox}"> 
      <Style.Triggers> 
       <Trigger Property="IsFocused" Value="True"> 
        <Setter Property="Background" Value="{DynamicResource FocusedBackground}" /> 
        <Setter Property="Foreground" Value="{DynamicResource FocusedForeground}" /> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </UserControl.Resources> 

    <Grid Background="{Binding Path=GridBackground, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}" 
      Width="{Binding Path=Width, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="*" /> 
      <ColumnDefinition Width="Auto" /> 
      <ColumnDefinition Width="Auto" /> 
     </Grid.ColumnDefinitions> 

     <ComboBox Background="{Binding Path=Background, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}" 
        BorderBrush="{Binding Path=BorderBrush, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}" 
        DisplayMemberPath="{Binding Path=DisplayMemberPath, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}" 
        FlowDirection="{Binding Path=FlowDirection, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}" 
        Focusable="True" 
        FontFamily="{Binding Path=FontFamily, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}" 
        FontSize="{Binding Path=FontSize, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}" 
        FontStretch="{Binding Path=FontStretch, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}" 
        FontStyle="{Binding Path=FontStyle, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}" 
        FontWeight="{Binding Path=FontWeight, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}" 
        Foreground="{Binding Path=Foreground, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}" 
        HorizontalAlignment="{Binding Path=HorizontalAlignment, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}" 
        HorizontalContentAlignment="{Binding Path=HorizontalContentAlignment, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}" 
        Grid.Column="0" 
        ItemsSource="{Binding Path=ItemsSource, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}" 
        Name="ChoicesPicker" 
        SelectedIndex="{Binding Path=SelectedIndex, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}" 
        SelectedValue="{Binding Path=SelectedValue, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}" 
        SelectedValuePath="{Binding Path=SelectedValuePath, Mode=TwoWay,RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}" 
        SelectionChanged="ChoicesPicker_SelectionChanged" 
        Style="{StaticResource FocusedStyle}" 
        TabIndex="{Binding Path=TabIndex, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}" 
        VerticalAlignment="Center"/> 
     <RepeatButton Background="{DynamicResource ButtonBackground}" 
         Click="SelectPreviousButton_Click" 
         Focusable="False" 
         Foreground="{DynamicResource ButtonForeground}" 
         Grid.Column="1" 
         IsTabStop="False" 
         Name="SelectPreviousButton"> 
      <Image Source="/CustomControls;component/Resources/VolumeUp.png" /> 
     </RepeatButton> 
     <RepeatButton Background="{DynamicResource ButtonBackground}" 
         Click="SelectNextButton_Click" 
         Focusable="False" 
         Foreground="{DynamicResource ButtonForeground}" 
         Grid.Column="2" 
         IsTabStop="False" 
         Name="SelectNextButton"> 
      <Image Source="/CustomControls;component/Resources/VolumeDown.png" /> 
     </RepeatButton> 
    </Grid> 
</UserControl> 

Ich möchte den Hintergrund und Vordergrund Bürsten zu ändern, wenn die ComboBox den Fokus erhält. Der Style namens "FocusedStyle" in den UserControl Resources soll das für mich tun, aber es funktioniert nicht. Die Farben Background und ForeGround ändern sich nie.

Was ist falsch an meinem Code?

Tony

Antwort

1

Sie können nicht eine Eigenschaft von einem Stil gesetzt, wenn diese Eigenschaft in der eigentlichen Steuerung gesetzt ist, weil jede Eigenschaft in der Steuerung eingestellte Vorrang.

Versuchen Sie folgendes:

<UserControl.Resources> 
    <Style x:Key="FocusedStyle" TargetType="{x:Type ComboBox}"> 
     <Style.Triggers> 
      <Trigger Property="IsFocused" Value="True"> 
       <Setter Property="Background" Value="{DynamicResource FocusedBackground}" /> 
       <Setter Property="Foreground" Value="{DynamicResource FocusedForeground}" /> 
      </Trigger> 
      <Trigger Property="IsFocused" Value="False"> 
       <Setter Property="Background" Value="{DynamicResource UnFocusedBackground}" /> 
       <Setter Property="Foreground" Value="{DynamicResource UnFocusedForeground}" /> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
</UserControl.Resources> 

Dann müssen Sie eine Dynamic der normalen Hintergrundinformationen.

dann die

Background="{Binding Path=Background, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}" 

und die

Foreground="{Binding Path=Foreground, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}" 

aus der Combobox entfernen.

Auf diese Weise kann der Trigger den Wert der Eigenschaft festlegen, da die Eigenschaft nicht vom Steuerelement selbst festgelegt wird.

+0

Danke, das war's! –

Verwandte Themen