2

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

我想实现类似于 Microsoft Word 中的“打印 n 页、当前页或选择”的东西。

Microsoft Word 打印 n 页

在我的示例中,我有单选按钮可以在radio1radio2之间切换,但也

  1. 默认为视图模型中的任何 TimeType
    1. TimeType == Type1=> radio1 被选中,text1 = ""
    2. TimeType == Type2=> radio2 被选中,text1 = ViewModel.Time
  2. 如果用户选择 radio1我想清除text1
  3. 如果在text1中输入值,我希望单选按钮切换到radio2并相应更新视图模型

我已经看到并尝试了各种转换器示例,但我无法弄清楚如何使对行为的各种影响能够很好地一起发挥作用。

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

XAML

<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>

查看模型

// 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;
}
4

2 回答 2

0

通常做这样的事情我使用一个ListBox(因为你想跟踪一个选定的项目,并且一次只能选择一个项目),并覆盖样式以显示为RadioButtons

<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 的一个示例:

<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>
于 2012-06-15T13:07:15.067 回答
0

你需要INotifyPropertyChanged在你身上实施ViewModel

于 2012-06-15T11:59:31.560 回答