2010-04-09 60 views
0

我试图从样式trigger.It工作,只要我们不手动更改组合框中的任何值设置组合框的选定值。但它手动更改选择后完全停止工作。我该如何解决this.A示例代码attached.Please确实有助于ComboBox选择更改中断DataTrigger

<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> 
+0

请更加详细一些。你想要的行为是什么?只要用户改变了这个值,触发器就应该启动,这使得它成为一个只读组合框? 另外,修复代码示例。 – 2010-04-09 07:24:08

+0

我想清除组合框的选择,同时禁用组合框。如果数据是通过使用style.But设置,但如果我们更改从代码隐藏的组合框选择,或者如果我们更改选择从那么触发器就会屈服于工作。 – biju 2010-04-09 09:15:11

回答

0

我已经找到了快速而肮脏的Solutuin我guess..If任何人有一个更好的主意,请..

<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>