kyl*_*yle 3 data-binding wpf xaml styles
我将一组对象绑定到WPF中的列表框,为简单起见,我们会说我绑定的对象有3个属性:Name,URL,IsBold.我想要做的是,如果将IsBold设置为true,则将其显示为不同,再次作为示例我想将Name出现在其中的TextBlock设置为粗体.这样的事情甚至可能吗?如果我的某个属性是某个值,我可以使用不同的样式吗?(我可以像XAML中的if/else那样做什么)?我真的不知道从哪里开始.
说我在我的DataTemplate中有这个
<TextBlock Style="{StaticResource notBold}" Text="{Binding Path=Name}"></TextBlock>
Run Code Online (Sandbox Code Playgroud)
如果IsBold对于特定的项目设置为true我希望它是(注意样式从'notBold'变为'isBold')
<TextBlock Style="{StaticResource isBold}" Text="{Binding Path=Name}"></TextBlock>
Run Code Online (Sandbox Code Playgroud)
或类似的东西.我想更普遍的问题.是否有可能根据数据绑定的项目更改某些内容的外观?如果不可能,这样的事情怎么样呢?以某种方式通过代码隐藏?
谢谢
您通常要做的是为列表中的对象编写DataTemplate,然后让DataTrigger根据IsBold属性设置TextBlock/TextBox的Fontweight.
<DataTemplate DataType="DataItem">
<TextBlock x:Name="tb" Text="{Binding Name}"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsBold}" Value="true">
<Setter TargetName="tb" Property="FontWeight" Value="Bold" />
</DataTrigger>
<DataTrigger Binding="{Binding IsBold}" Value="false">
<Setter TargetName="tb" Property="FontWeight" Value="Normal" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
然后,您可以将DataItems列表设置为ComboBox的ItemsSource属性(通过Databinding或直接在代码隐藏中myComboBox.ItemsSource=myDataItems).其余的由WPF为您完成.
| 归档时间: |
|
| 查看次数: |
3364 次 |
| 最近记录: |