相关疑难解决方法(0)

如何将RadioButtons绑定到枚举?

我有这样的枚举:

public enum MyLovelyEnum
{
    FirstSelection,
    TheOtherSelection,
    YetAnotherOne
};
Run Code Online (Sandbox Code Playgroud)

我的DataContext中有一个属性:

public MyLovelyEnum VeryLovelyEnum { get; set; }
Run Code Online (Sandbox Code Playgroud)

我的WPF客户端中有三个RadioButton.

<RadioButton Margin="3">First Selection</RadioButton>
<RadioButton Margin="3">The Other Selection</RadioButton>
<RadioButton Margin="3">Yet Another one</RadioButton>
Run Code Online (Sandbox Code Playgroud)

现在如何将RadioButtons绑定到属性以进行正确的双向绑定?

data-binding wpf enums radio-button

400
推荐指数
4
解决办法
13万
查看次数

如何覆盖ListBox的ItemTemplate并仍保留DisplayMemberPath?

我有一个通用的样式ListBox覆盖ItemTemplate使用RadioButtons.它工作得很好,除了我设置时DisplayMemberPath.然后我就得到了.ToString()该项目的内容ListBox.

我觉得我在这里缺少一些简单的东西......有人可以帮助我发现它吗?

<Style x:Key="RadioButtonListBoxStyle" TargetType="{x:Type ListBox}">
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="KeyboardNavigation.DirectionalNavigation" Value="Cycle" />
    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <Style TargetType="{x:Type ListBoxItem}" >
                <Setter Property="Margin" Value="2, 2, 2, 0" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Border Background="Transparent">
                                <RadioButton
                                    Content="{TemplateBinding ContentPresenter.Content}" VerticalAlignment="Center"
                                    IsChecked="{Binding Path=IsSelected,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}"/>

                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

ListBox绑定到List<T>KeyValuePairs.如果我删除了样式,则DisplayMemberPath显示正确,因此它必须是样式的东西.

<ListBox Style="{StaticResource RadioButtonListBoxStyle}"
         ItemsSource="{Binding MyCollection}"
         DisplayMemberPath="Value" SelectedValuePath="Key" />
Run Code Online (Sandbox Code Playgroud)

wpf xaml templates listbox

10
推荐指数
2
解决办法
4287
查看次数

WPF + MVVM + RadioButton:处理与单个属性的绑定

这个以及关于SO的这个(和其他)问题以及互联网上的许多其他材料,我理解了如何将单选按钮与VM绑定.

但是所有这些都为单选按钮的每个可能值创建了单独的属性.一个问题(这个)类似于我的要求,但接受的答案建议使用ListBox而不是单选按钮.

要表示人员性别(数据类型CHAR,可能值'M','F'),需要在VM中创建三个属性,如PersonGender,IsPersonMale,IsPersonFemale.我想只在一个属性PersonGender上控制它.我可以这样做吗?如果有,怎么样?

c# data-binding wpf mvvm radio-button

8
推荐指数
1
解决办法
1万
查看次数

标签 统计

wpf ×3

data-binding ×2

radio-button ×2

c# ×1

enums ×1

listbox ×1

mvvm ×1

templates ×1

xaml ×1