views:

31

answers:

1

Hi, I am trying to set the selected value of a combobox from a style trigger.It works as long as we dont manually change any value in the combobox.But it stops working altogether after manually changing the selection.How can i solve this.A sample code is attached.Please do help

<Window x:Class="InputGesture.Window2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:InputGesture"
    Title="Window2" Height="300" Width="300" Name="Sample">
    <Window.Resources>
        <Style TargetType="{x:Type ComboBox}">
            <Setter Property="ComboBox.SelectedValue" Value="1"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=chk,Path=IsChecked}" Value="True">
                    <Setter Property="ComboBox.IsEnabled" Value="False"/>
                    <Setter Property="ComboBox.SelectedValue" Value="2"/>
                </DataTrigger>
            </Style.Triggers>
            </Style>
    </Window.Resources>
    <StackPanel>
        <CheckBox Name="chk" Height="23"/>
        <ComboBox Name="cmb" Height="23" DisplayMemberPath="Name"             
                  SelectedValuePath="Id" ItemsSource="{Binding ElementName=Sample,Path=DT}">
        </ComboBox>

        <Button Height="23" Click="Button_Click"/>
    </StackPanel>
</Window>
A: 

I have found a Quick but Dirty Solutuin i guess..If anyone has a better Idea Please..

<Window x:Class="InputGesture.Window2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:InputGesture"
    Title="Window2" Height="300" Width="300" Name="Sample">
    <Window.Resources>
     <Style TargetType="{x:Type ComboBox}">
         <Style.Resources>
                <Storyboard x:Key="valueStoryBoard" >

                    <ObjectAnimationUsingKeyFrames Duration="00:00:00"
                                                   FillBehavior="HoldEnd"
                                                   Storyboard.TargetProperty="SelectedValue">
                        <DiscreteObjectKeyFrame KeyTime="00:00:00"
                                                Value="{x:Null}" />
                        <!--<DiscreteObjectKeyFrame KeyTime="00:00:00"
                                                Value="2" />-->

                    </ObjectAnimationUsingKeyFrames>
                </Storyboard>
            </Style.Resources>
             <Style.Triggers>
            <DataTrigger Binding="{Binding IsChecked, ElementName=chk}"
                         Value="True">
                <DataTrigger.EnterActions>
                    <BeginStoryboard x:Name="stb" Storyboard="{StaticResource valueStoryBoard}" />
                </DataTrigger.EnterActions>
                <Setter Property="ComboBox.IsEnabled" Value="False"/>
                <DataTrigger.ExitActions>
                <StopStoryboard BeginStoryboardName="stb" />
                </DataTrigger.ExitActions>
            </DataTrigger>
        </Style.Triggers>
            </Style>

    </Window.Resources>
    <StackPanel>
        <CheckBox Name="chk" Height="23"/>
        <ComboBox Name="cmb" Height="23" DisplayMemberPath="Name"
                 SelectedValuePath="Id" ItemsSource="{Binding ElementName=Sample,Path=DT}">
        </ComboBox>

        <Button Height="23" Click="Button_Click"/>
    </StackPanel>
</Window>
biju