首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于多个标准的单选按钮和文本框绑定

基于多个标准的单选按钮和文本框绑定
EN

Stack Overflow用户
提问于 2012-06-15 11:49:23
回答 2查看 1.9K关注 0票数 2

我是WPF和MVVM的新手,我正在寻找正确方向的指针。

我希望实现类似于Microsoft中的“打印n页、当前页面或选择”的内容。

在我的例子中,我有收音机按钮在radio1或radio2之间切换,但也有

默认情况下,视图模型中的

  1. TimeType == Type1 => radio1被选中,text1 = ""
  2. TimeType == Type2 => radio2被选中,text1 = ViewModel.Time

  1. 如果用户选择 radio1,则清除text1 .
  2. ,如果在text1中输入值,则希望单选按钮切换到radio2,视图模型相应更新

G 222

我见过并尝试过各种变流器的例子,但我想不出如何使各种对这种行为的影响很好地结合在一起。

我有一种感觉,我应该在视图模型中实现一些东西来执行逻辑,但我看不出绑定到什么。

XAML

代码语言:javascript
复制
<Grid.Resources>
    <local:EnumToBooleanConverter x:Key="e2b" />
</Grid.Resources>
<RadioButton Name="radio1" GroupName="g1" Content="Radio 1"
    IsChecked="{Binding Path=TimeType, Converter={StaticResource e2b}, ConverterParameter={x:Static vm:TimeType.Type1}}"
    />
<RadioButton Name="radio2" GroupName="g1">
    <TextBox Name="text1"
        Text="{Binding Path=ExplicitTime, Mode=TwoWay}">
    </TextBox>
</RadioButton>

视图模型

代码语言:javascript
复制
// UPDATE: added INotifyPropertyChanged as per my actual code
class MagicTimeViewModel :INotifyPropertyChanged {
    public enum TimeType{Type1, Type2}
    TimeType _type; int _time;
    public TimeType TimeType{
        get{_return _type;}
        set {_type = value; Notify("TimeType");}
    }
    public int Time {
        get{_return _time;}
        set {_time = value; Notify("Time");}
    }
    void Notify(string name)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(name));
    }
    public event PropertyChangedEventHandler PropertyChanged;
}
EN

回答 2

Stack Overflow用户

发布于 2012-06-15 11:59:31

您需要在您的INotifyPropertyChanged中实现ViewModel

票数 0
EN

Stack Overflow用户

发布于 2012-06-15 13:07:15

通常,为了这样做,我使用ListBox (因为您希望跟踪所选的项,一次只能选择一个项),并覆盖要显示为RadioButtons的样式。

代码语言:javascript
复制
<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 IsHitTestVisible="False" Focusable="false" 
                                    Content="{TemplateBinding ContentPresenter.Content}"  
                                    IsChecked="{Binding Path=IsSelected,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Setter.Value>
    </Setter>
</Style>

ViewModel存储SelectedIndexTimeText属性,当一个属性更改时,它还更新任何相关属性。

例如,当SelectedIndex更改为0时,清除TimeText。当TimeText更改时,将SelectedIndex设置为1

下面是XAML的一个示例:

代码语言:javascript
复制
<ListBox x:Name="MyListBox"
         Style="{StaticResource RadioButtonListBoxStyle}"
         SelectedIndex="{Binding SelectedIndex}">

    <ListBoxItem>All</ListBoxItem>
    <ListBoxItem>
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="Pages" />
            <TextBox Text="{Binding DataContext.TimeText, ElementName=MyListBox, UpdateSourceTrigger=PropertyChanged}" />
        </StackPanel>
    </ListBoxItem>
</ListBox>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11050126

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档