【问题标题】:Change style of RadioButton depending on property根据属性更改 RadioButton 的样式
【发布时间】:2016-08-16 16:40:50
【问题描述】:

我有一个作为 ItemsControl DataTemplate 的一部分动态设置的 RadioButton。

<RadioButton GroupName="Ratings">
  <RadioButton.Content>
    <StackPanel Orientation="Horizontal">
      <TextBlock Text="{Binding Score}" />
      <TextBlock Text=" - " />
      <TextBlock Text="{Binding Description}" />
    </StackPanel>
  </RadioButton.Content>
</RadioButton>

我有两个预定义的样式(MyCheckedStyle1MyUncheckedStyle2),当我设置 RadioButton 的 Style= 属性时,它们单独工作正常,但我还没有找到基于 IsChecked 属性更改样式的方法。

我尝试尝试的大多数方法都给我一个关于 Style object is not allowed to affect the Style property of the object to which it applies. 的异常(例如 ContentTemplate 触发器)

所以伪代码

if IsChecked = true then 
  style = MyCheckedStyle1
else if IsChecked = false then
  style = MyUncheckedStyle1

我可以使用代码隐藏来做到这一点,但我会尽可能避免这种情况,并将逻辑放在 XAML 中。

【问题讨论】:

  • 将样式应用于收音机的Parent,然后使用触发器通过 IsChecked 属性更改收音机的样式。另一种将样式应用于收音机本身的方法,然后使用触发器通过 IsChecked 属性更改 ControlTemplate。
  • 样式不是我定义的,我无法更改它们。您的第一个选项听起来可行,您能否提供一个答案来进一步解释您的意思?
  • 你不能只改变 Checked/Unchecked 事件代码中的样式吗?
  • 是的,我在我的问题中提到它可以在后面的代码中完成,想要在视图上执行此操作,而不是 @codeDom 所示

标签: c# wpf xaml


【解决方案1】:

将样式应用于收音机的父级,然后使用触发器通过 IsChecked 属性更改收音机的样式。

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="101" Width="264">
    <Window.Resources>

        <Style x:Key="MyCheckedStyle1" TargetType="RadioButton">
            <Setter Property="Background" Value="Red"/>
        </Style>
        <Style x:Key="MyCheckedStyle2" TargetType="RadioButton">
            <Setter Property="Background" Value="Blue"/>
        </Style>

        <Style x:Key="ParentStyle" TargetType="ContentControl">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <RadioButton Name="RadioButton1" GroupName="Ratings" >
                            <RadioButton.Content>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="{Binding Score}" />
                                    <TextBlock Text=" - " />
                                    <TextBlock Text="{Binding Description}" />
                                </StackPanel>
                            </RadioButton.Content>
                        </RadioButton>
                        <ControlTemplate.Triggers>
                            <Trigger SourceName="RadioButton1" Property="IsChecked" Value="True">
                                <Setter TargetName="RadioButton1" Property="Style" 
                                        Value="{StaticResource MyCheckedStyle1}"/>
                            </Trigger>
                            <Trigger SourceName="RadioButton1" Property="IsChecked" Value="False">
                                <Setter TargetName="RadioButton1" Property="Style" 
                                        Value="{StaticResource MyCheckedStyle2}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <ListBox>
            <ContentControl Style="{StaticResource ParentStyle}"/>
            <ContentControl Style="{StaticResource ParentStyle}"/>
        </ListBox>
    </Grid>
</Window>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-08
    • 2016-12-12
    • 1970-01-01
    • 2018-08-26
    • 2012-04-25
    • 2012-06-10
    • 2021-12-13
    相关资源
    最近更新 更多