我有2个布局,一个用于肖像,一个用于forlandscape。
在每个布局中,我都有一个ItemsControl
,其中包含RadioButton
个集合。
如果我从一个方向切换到另一个方向,有时候应该选中RadioButton
时取消选中,因此我的RadioButton
集合中不包含选中的按钮。
两个方向都显示相同的数据,只是布局发生了变化。
请注意IsChecked
绑定:IsChecked="{Binding IsSelected, Mode=TwoWay}">
纵向布局
<Grid x:Name="SymbolsGridPortrait" Grid.Row="1">
......
<ScrollViewer ZoomMode="Disabled">
<Grid x:Name="SymbolsContentGridPortrait">
<ItemsControl ItemsSource="{Binding SymbolsItems}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<RadioButton GroupName="SymbolsRadioGroupName"
FontSize="20"
Foreground="Black"
Margin="10,0,0,0"
IsChecked="{Binding IsSelected, Mode=TwoWay}">
<TextBlock Text="{Binding Name}" TextWrapping="Wrap" />
</RadioButton>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid>
</ScrollViewer>
景观布局
<Grid x:Name="SymbolGridLandscape" Background="LightGray" Grid.Row="1">
.....
<ScrollViewer ZoomMode="Disabled">
<Grid x:Name="SymbolsContentGrid">
<ItemsControl ItemsSource="{Binding SymbolsItems}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<RadioButton GroupName="SymbolsRadioGroupName"
FontSize="20"
Foreground="Black"
Margin="10,0,0,0"
IsChecked="{Binding IsSelected, Mode=TwoWay}">
<TextBlock Text="{Binding Name}" TextWrapping="Wrap" />
</RadioButton>
</DataTemplate>
.....
视图模型:
internal ObservableCollection<SymbolItem> _symbolsItems;
public ObservableCollection<SymbolItem> SymbolsItems
{
get
{
return _symbolsItems;
}
set
{
_symbolsItems = value;
}
}
型号:
public class SymbolItem : Common.BindableBase
{
....
internal bool _isSelected;
public virtual bool IsSelected
{
get
{
return _isSelected;
}
set
{
_isSelected = value;
}
}
}
VisualStateManager:
<VisualStateManager.VisualStateGroups>
<!-- Visual states reflect the application's view state -->
<VisualStateGroup x:Name="ApplicationViewStates">
<VisualState x:Name="FullScreenLandscape">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="SearchBodyGridLandscape"
Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0" Value="Visible" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="SearchBodyGridPortrait"
Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Filled"/>
<!-- The entire page respects the narrower 100-pixel margin convention for portrait -->
<VisualState x:Name="FullScreenPortrait">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PortraitBackButtonStyle}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="SearchBodyGridLandscape"
Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="SearchBodyGridPortrait"
Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0" Value="Visible" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
答案 0 :(得分:1)
虽然之前的答案仍然存在,并且您应该更新,但还有一个问题。您已将所有RadioButton
设置为位于同一组中!更改下面列出的Setter,但也为每个Landscape和Portrait创建一个单独的组(通过GroupName
)属性。
E.g。
<RadioButton GroupName="SymbolsRadioButtonsPortrait"
.../>
........
<RadioButton GroupName="SymbolsRadioButtonsLandscape"
.../>
请记住,您需要同时执行这两个答案,这样当您更改一个值时,它不会取消选择所有其他RadioButtons,和之前的答案,以便模型正确更新其他小组的控制。
上一个回答:
使用BindableBase
,他们为您构建了一个setter方法,SetProperty
(SetProperty<T>(ref T storage, T value
)。
像这样改变你的模型:
set
{
SetProperty<bool>(ref _isSelected, value);
}
这将导致触发继承的PropertyChanged
事件,通知用户界面。