WPF - Triggers

使用control自由的property作为trigger
ContractedBlock.gifExpandedBlockStart.gifView Code
<ToggleButton Width="30" Height="30">
<ToggleButton.Style>
<Style TargetType="ToggleButton">
<Style.Triggers>
<Trigger Property="IsChecked" Value="true">
<Setter Property="Content">
<Setter.Value>
<Border BorderThickness="1" BorderBrush="Blue">
<Image Source="LimitChecked.ico"/>
</Border>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsChecked" Value="false">
<Setter Property="Content">
<Setter.Value>
<Image Source="LimitUnChecked.ico"/>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</ToggleButton.Style>
</ToggleButton>
-
使用其他control的property作为trigger
ContractedBlock.gifExpandedBlockStart.gifView Code
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">

<CheckBox x:Name="theCB" Content="Enable the TextBox"/>

<TextBox><!--can NOT set 'IsEnabled' property in this line anymore, that will overwrite the style-->
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=theCB, Path=IsChecked}" Value="True">
<Setter Property="IsEnabled" Value="True"/>
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=theCB, Path=IsChecked}" Value="False">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>

</StackPanel>
-
使用ViewModel中的property作为trigger
ContractedBlock.gifExpandedBlockStart.gifView Code
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">

<CheckBox IsChecked="{Binding IsRed}" Content="IsRed"/>

<TextBlock>
<TextBlock.Style>
<Style><!--is not specify target type-->
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsRed}" Value="True">
<Setter Property="TextBlock.Background" Value="Red"/><!--then, need prefixed with 'TextBlocl.' which is the target type-->
</DataTrigger>
<DataTrigger Binding="{Binding Path=IsRed}" Value="False">
<Setter Property="TextBlock.Background" Value="Black"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</StackPanel>
-
tbd...

转载于:https://www.cnblogs.com/yapzhang/archive/2010/09/01/1814519.html

### WPF 中 RowStyle 的使用方法和示例 在 WPF 中,`RowStyle` 是 `DataGrid` 控件的一个重要属性,用于定义数据网格中每一行的样式。通过设置 `RowStyle`,可以实现诸如更改背景颜色、字体样式、鼠标悬停效果等功能[^1]。 下面是一个完整的示例,展示如何在 WPF 中使用 `RowStyle` 来自定义 `DataGrid` 行的外观: ```xml <Window.Resources> <!-- 定义一个 Style 用于 DataGrid 的 Row --> <Style x:Key="CustomRowStyle" TargetType="DataGridRow"> <Setter Property="Background" Value="LightGray"/> <Setter Property="Foreground" Value="Black"/> <Setter Property="FontWeight" Value="Bold"/> <Setter Property="Height" Value="30"/> <Style.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter Property="Background" Value="Blue"/> <Setter Property="Foreground" Value="White"/> </Trigger> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="LightBlue"/> </Trigger> </Style.Triggers> </Style> </Window.Resources> <DataGrid Name="dataGrid" AutoGenerateColumns="True" RowStyle="{StaticResource CustomRowStyle}"> <!-- 数据源绑定等其他配置 --> </DataGrid> ``` 在这个示例中: - 使用了 `<Style>` 元素来定义 `DataGridRow` 的样式。 - 设置了背景颜色 (`Background`)、前景颜色 (`Foreground`)、字体粗细 (`FontWeight`) 和行高 (`Height`)。 - 使用 `Style.Triggers` 来定义交互行为,例如当行被选中时 (`IsSelected`) 或鼠标悬停时 (`IsMouseOver`) 的效果。 此外,还可以结合数据触发器(`DataTrigger`)根据绑定的数据值动态调整行样式。例如,以下代码展示了如何根据某列的值更改行的颜色: ```xml <Style x:Key="ConditionalRowStyle" TargetType="DataGridRow"> <Style.Triggers> <DataTrigger Binding="{Binding Status}" Value="Active"> <Setter Property="Background" Value="Green"/> </DataTrigger> <DataTrigger Binding="{Binding Status}" Value="Inactive"> <Setter Property="Background" Value="Red"/> </DataTrigger> </Style.Triggers> </Style> ``` 在此示例中,`Status` 列的值决定了行的背景颜色。如果 `Status` 的值为 "Active",则行的背景颜色为绿色;如果值为 "Inactive",则背景颜色为红色。 ### 注意事项 - 确保 `RowStyle` 的 `TargetType` 被正确设置为 `DataGridRow`,否则样式可能无法生效。 - 如果需要更复杂的样式,可以考虑使用 `ControlTemplate` 来完全重行的外观[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值