【问题标题】:WPF RadioButton styled as ToggleButton not displaying initial valueWPF RadioButton 样式为 ToggleButton 不显示初始值
【发布时间】:2015-11-19 19:35:33
【问题描述】:

我将三个单选按钮(使用 Caliburn.Micro)绑定到视图模型的三个属性。所有属性似乎都正确,我调试以查看所选属性返回 True 并且控件具有 IsChecked = true 并且属性通知器似乎正在提高,但是当我第一次打开表单时,没有任何切换按钮出现选中.如果我开始单击它们,一切正常,只有在初始加载时所选值没有被“按下”。这是 XAML...

<RadioButton GroupName="Alignment" Name="FooterLeftAlign" Style="{StaticResource {x:Type ToggleButton}}" Margin="0,0,5,0">
    <Image Source="pack://application:,,,/DevExpress.Images.v15.1;component/Images/Format/AlignLeft_16x16.png" Margin="0" />
</RadioButton>
<RadioButton GroupName="Alignment" Name="FooterCenterAlign" Style="{StaticResource {x:Type ToggleButton}}" Margin="0,0,5,0">
    <Image Source="pack://application:,,,/DevExpress.Images.v15.1;component/Images/Format/AlignCenter_16x16.png"  Margin="0" />
</RadioButton>
<RadioButton GroupName="Alignment" Name="FooterRightAlign" Style="{StaticResource {x:Type ToggleButton}}" Margin="0,0,5,0">
    <Image Source="pack://application:,,,/DevExpress.Images.v15.1;component/Images/Format/AlignRight_16x16.png" Margin="0" />
</RadioButton>

这里是属性...

public bool FooterLeftAlign {
        get {
            return (ScannerSettings.StampOptions.TextAlign == TextAlignment.Left);
        }
        set {
            if (value) {
                ScannerSettings.StampOptions.TextAlign = TextAlignment.Left;
                RaisePropertyChanged(() => FooterLeftAlign);
            }
        }
    }

    public bool FooterCenterAlign {
        get {
            return (ScannerSettings.StampOptions.TextAlign == TextAlignment.Center);
        }
        set {
            if (value) {
                ScannerSettings.StampOptions.TextAlign = TextAlignment.Center;
                RaisePropertyChanged(() => FooterCenterAlign);
            }
        }
    }

    public bool FooterRightAlign {
        get {
            return (ScannerSettings.StampOptions.TextAlign == TextAlignment.Right);
        }
        set {
            if (value) {
                ScannerSettings.StampOptions.TextAlign = TextAlignment.Right;
                RaisePropertyChanged(() => FooterRightAlign);
            }
        }
    }

我什至在这个表单上有一些实际的切换按钮,用于粗体、斜体等......它们工作正常。我已经看到很多使用触发器强制自定义样式的示例,但我想确保它看起来像其他切换按钮。由于我见过与此非常相似的其他示例,我唯一能想到的是我们有一个 DevExpress 主题,但找不到任何看起来会改变这种行为的东西。

如果有人有任何建议,将不胜感激。

【问题讨论】:

  • 那些单选按钮似乎没有将 IsChecked 绑定到任何东西...您是在其他地方设置它们的值吗?
  • 抱歉,感谢您指出这一点,它们是使用 Caliburn.Micro 绑定的。

标签: c# wpf xaml devexpress caliburn.micro


【解决方案1】:

如果这是代码隐藏,您可以设置 ((RadioButton)FooterRightAlign).IsChecked = value。如果这实际上是 MVVM 情况,则需要将每个单选按钮绑定到这些布尔值。

<RadioButton GroupName="Alignment" Name="FooterRightAlign" Style="{StaticResource {x:Type ToggleButton}}" IsChecked="{Binding FooterRightAlign, Mode=TwoWay}" Margin="0,0,5,0">
<Image Source="pack://application:,,,/DevExpress.Images.v15.1;component/Images/Format/AlignRight_16x16.png" Margin="0" />

【讨论】:

  • 绑定来自 CalibrunMicro 并且在单击按钮时似乎工作正常 我在加载期间尝试手动设置 IsClicked 并且它似乎仍然没有相应地更新 UI。我在对话框加载期间看到它查询我的属性并取回正确的值,并且 IsClicked 甚至在控件上似乎是正确的。就像样式中的某些内容没有正确触发一样。
  • ((RadioButton)FooterRightAlign).IsChecked = value;不起作用,因为您有与 bool 属性命名相同的单选按钮。
  • 我不熟悉 CalibrunMicro,所以这对我来说似乎很陌生。
  • 它只是一个自动绑定事物的框架,例如FooterRightAlign ui 元素自动绑定到 {DataContext}.FooterRightAlign 属性。
  • 我会尝试在后面的代码中对 Window.Loaded 事件设置 IsChecked。 public MainWindow(){this.Loaded += MainWindow_Loaded;} 然后在 MainWindow_Loaded(object sender, RoutedEventArgs e){ ((RadioButton)FooterRightAlign.IsChecked = ((ViewModelType)this.DataContext).FooterRightAlign;}
猜你喜欢
  • 2013-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-27
  • 2017-08-20
  • 1970-01-01
  • 2013-05-31
相关资源
最近更新 更多