2012-11-20 104 views
1

我是新来的WPF蜜蜂。我有组合框的问题。风格触发组合框WPF

当组合框渲染后的第一时间并没有任何显示,组合框的边框必须设置为红色,但遗憾的是它没有这样做。

请找到代码片段

<Style x:Key="requiredFieldValidationStyleComboBox"> 
    <Style.Triggers> 
     <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=SelectedItem.Content}" Value="{x:Null}"> 
      <Setter Property="ComboBox.BorderBrush" Value="{StaticResource FaultyBorderBrush}" /> 
      <Setter Property="ComboBox.ToolTip" Value="Input value is mandatory" /> 
     </DataTrigger> 
    </Style.Triggers> 
</Style> 
+0

什么是绑定的SelectedItem对象?它有内容属性呢? – Harry

+0

我确定你已经检查了这个,但是你把样式放在组合框上了吗? Andy

回答

0

我不知道什么对象绑定到selectedItem属性。 但我有你要问,如果本身的SelectedItem等于空的感觉, 然后代码应该是这样的:

<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=SelectedItem}" Value="{x:Null}"> 
       <Setter Property="ComboBox.BorderBrush" Value="{StaticResource FaultyBorderBrush}" /> 
       <Setter Property="ComboBox.ToolTip" Value="Input value is mandatory" /> 
      </DataTrigger> 

“内容”是不必要的。

这是你想要的吗?

0

我发现你的风格工作完美,一旦我将它添加到组合框,确保组合框实际上有它的样式属性设置或删除x:Key属性,如果你想它适用于所有组合框。

<Window.Resources> 
     <Style x:Key="requiredFieldValidationStyleComboBox"> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=SelectedItem.Content}" Value="{x:Null}"> 
        <Setter Property="ComboBox.BorderBrush" Value="Red" /> 
        <Setter Property="ComboBox.ToolTip" Value="Input value is mandatory" /> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </Window.Resources> 
    <Grid Margin="0,0,401,279"> 
     <ComboBox Style="{StaticResource requiredFieldValidationStyleComboBox}" Margin="46,66,-46,-66"/> 
    </Grid> 

Nothing selected

Selected