C# 2项控件和一个数据源
我有两个布局,一个用于纵向布局,一个用于横向布局 在每个布局中,我都有一个C# 2项控件和一个数据源,c#,wpf,windows,windows-store-apps,winrt-xaml,C#,Wpf,Windows,Windows Store Apps,Winrt Xaml,我有两个布局,一个用于纵向布局,一个用于横向布局 在每个布局中,我都有一个ItemsControl和一组RadioButtons。 如果我从一个方向切换到另一个方向,有时应选中的RadioButton未选中,因此我的RadioButton集合不包含选中的按钮 两个方向显示相同的数据,只是布局有所改变 注意IsCheckedbinding:IsChecked=“{binding IsSelected,Mode=TwoWay}”> 纵向布局 <Grid x:Name="SymbolsGridP
ItemsControl
和一组RadioButton
s。
如果我从一个方向切换到另一个方向,有时应选中的RadioButton
未选中,因此我的RadioButton
集合不包含选中的按钮
两个方向显示相同的数据,只是布局有所改变
注意IsChecked
binding: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>
.....
VisualStateManager:
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.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>
虽然前面的答案仍然正确,您应该更新,但还有一个问题。您已将所有
单选按钮设置为同一组!更改如下所列的Setter,但也要为每个横向和纵向创建一个单独的组(通过GroupName
)属性
例如
这将导致触发继承的PropertyChanged
事件,通知UI。我们需要查看模型本身。这里最可能的情况是,在设置IsSelected
时,您没有引发PropertyChanged
事件,因此绑定到该属性的其他控件没有收到已发生更改的通知。@NateDiamond-我已编辑了文章,并包含了来自dataSourceIt的一些代码。它不起作用。我使用调试器进行了测试,发现在旋转设备时,会调用IsSelected getter并为选定项返回true,但紧接着调用IsSelected setter时,为选定项返回FALSE值,因此不再选中任何单选按钮。单选按钮位于一个组中,操作系统会在选中另一个单选btn时自动取消选中该单选按钮。这里是取消选中相同的元素。可能肖像项控件没有选中任何单选项,调用IsSelected getter->检测一个项被选中,并以某种方式取消选中似乎是同一对象的旧值。这是因为有两个控件,其中一个在旋转设备后没有检查。我从pagePerfect添加了visualState。它可以工作……我是一名android开发人员,对windows环境不太熟悉,但你刚刚救了我:)
<RadioButton GroupName="SymbolsRadioButtonsPortrait"
.../>
........
<RadioButton GroupName="SymbolsRadioButtonsLandscape"
.../>
set
{
SetProperty<bool>(ref _isSelected, value);
}