【问题标题】:WPF - styling RadioButton as ToggleButton works, but shows error in VSWPF - 将 RadioButton 样式设置为 ToggleButton 有效,但在 VS 中显示错误
【发布时间】:2014-03-13 01:20:59
【问题描述】:

在 WPF 中创建顶级菜单时,我使用了将 RadioButton 样式设置为 ToggleButton 的技巧,以获得“仅选择一个”效果。像这样:

<ItemsControl ItemsSource="{Binding ViewModels}">
    <ItemsControl.ItemTemplate>
    <DataTemplate>
        <RadioButton  Content="{Binding Key.Name}" GroupName="MenuButtonGroup"
                      Style="{StaticResource {x:Type ToggleButton}}" >
        </RadioButton>
    </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

它运行良好,并且符合我的预期。但 Visual Studio 将其注册为错误。

Style 属性带有蓝色下划线,给出的描述是资源“{x:Type ToggleButton}”无法解析。

这一切看起来都是光明正大的,但是把它放在我在 Visual Stuido 的错误列表中是非常令人恼火的。知道如何解决吗?

编辑:刚刚发现这个问题 -

The resource could not be resolved (VS 2010 RC)

这表明这是一个 VS 错误。有人证实这一点,或者知道解决办法吗?哪里有问题,还是很烦人!

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

    您可以将样式移动到资源集合中,例如

    <Window.Resources>
                <Style x:Key="MyStyle" TargetType="{x:Type RadioButton}" BasedOn="{StaticResource {x:Type ToggleButton}}" />
    </Window.Resources>
    

    然后引用该样式:

    <RadioButton  Content="{Binding Key.Name}" GroupName="MenuButtonGroup"
                             Style="{StaticResource MyStyle}" />
    

    【讨论】:

    • 很好,谢谢。从未想过尝试这么简单的事情!
    猜你喜欢
    • 1970-01-01
    • 2013-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多