2010-04-09 39 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> 
相關問題