我是WPF和MVVM的新手,我正在寻找正确方向的指针。
我希望实现类似于Microsoft中的“打印n页、当前页面或选择”的内容。

在我的例子中,我有收音机按钮在radio1或radio2之间切换,但也有
默认情况下,视图模型中的
TimeType == Type1 => radio1被选中,text1 = ""TimeType == Type2 => radio2被选中,text1 = ViewModel.Time
G 222
我见过并尝试过各种变流器的例子,但我想不出如何使各种对这种行为的影响很好地结合在一起。
我有一种感觉,我应该在视图模型中实现一些东西来执行逻辑,但我看不出绑定到什么。
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;
}发布于 2012-06-15 11:59:31
您需要在您的INotifyPropertyChanged中实现ViewModel
发布于 2012-06-15 13:07:15
通常,为了这样做,我使用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存储SelectedIndex和TimeText属性,当一个属性更改时,它还更新任何相关属性。
例如,当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>https://stackoverflow.com/questions/11050126
复制相似问题