2012-05-07 60 views
0

如何在进行组合框选择时设置文本框的属性。例如,当组合框选择时,设置背景和文本框的IsEnabled属性。我想要它纯粹在XAML中不在代码后面。我使用MVVM当进行组合框选择时设置文本框的属性WPF XAML

+1

这个问题是相似的:http://stackoverflow.com/questions/2561820/wpf-visibility-of-基于元素的组合选择 – 2012-05-07 11:33:04

回答

0

如何启用textBox1中,只有当SelectedItems是1

<TextBox Height="23" HorizontalAlignment="Left" Margin="246,177,0,0" Name="textBox2" VerticalAlignment="Top" Width="120"> 
     <TextBox.Style> 
      <Style TargetType="{x:Type TextBox}"> 
       <Setter Property="IsEnabled" Value="False"></Setter> 
       <Style.Triggers> 
        <DataTrigger Binding="{Binding ElementName=comboBox1, Path=SelectedIndex}" Value="1"> 
         <Setter Property="Background" Value="Green"></Setter> 
         <Setter Property="IsEnabled" Value="True"></Setter> 
        </DataTrigger> 
       </Style.Triggers> 
      </Style> 
     </TextBox.Style> 
    </TextBox> 
    <ComboBox Height="22" HorizontalAlignment="Left" Margin="246,119,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" /> 

我只是想用XAML无法达到的条件值=“1”或“3”,即在数据触发的关系比平等更复杂。

对于这种情况,你需要一个转换器。 此链接可以帮助你

How to get DataTemplate.DataTrigger to check for greater than or less than?

+0

如何在数据触发器中有两个或多个条件,或为属性指定多个值,例如 。我想为SelectedIndex指定多个值 – user1379584

0

您可以使用组合的选择对象datatrigger。看看以前的这个问题:WPF Visibility of a UI element based on combo selection

尝试生成触发器时,selecteditem是{x:Null}。为此,您需要将控件放入DataTemplate中,并将触发器放入模板的触发器集合中。

这里是一个示例代码(未测试,请你自己检查):

<TextBox Height="23" HorizontalAlignment="Left" Margin="246,177,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" IsEnabled" Value="True" /> 

<ComboBox Height="22" HorizontalAlignment="Left" Margin="246,119,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" /> 

<DataTemplate.Triggers> 
    <Trigger SourceName="comboBox1" Property="ComboBox.SelectedItem" Value="{x:Null}"> 
     <Setter TargetName="textbox2" Property="TextBox.IsEnabled" Value="False" /> 
    </Trigger> 
</DataTemplate.Triggers> 
+0

我们可以这样做 <条件绑定=“{Binding ElementName = cmbInstrumentType,Path = SelectedIndex}”Value =“2”/> user1379584

+0

看起来像你正在采取逆逻辑。为什么不检查SelectedItem == null? – 2012-05-08 12:30:56

+0

你怎么可以给它代码 – user1379584