C# 4.0 Windows应用商店应用程序:获取是否选中单选按钮并获取要保存在数据库中的名称
我有一个用于windows应用商店应用程序的xaml页面。我想获得选中单选按钮的值和名称,然后写入数据库C# 4.0 Windows应用商店应用程序:获取是否选中单选按钮并获取要保存在数据库中的名称,c#-4.0,windows-store-apps,winrt-xaml,C# 4.0,Windows Store Apps,Winrt Xaml,我有一个用于windows应用商店应用程序的xaml页面。我想获得选中单选按钮的值和名称,然后写入数据库 <Page x:Name="pageRoot" x:Class="RadioButtons.MyRadio" DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}" xmlns="http://schemas.microsoft.com/winfx/20
<Page
x:Name="pageRoot"
x:Class="RadioButtons.MyRadio"
DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:RadioButtons"
xmlns:common="using:RadioButtons.Common"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
<!-- TODO: Delete this line if the key AppName is declared in App.xaml -->
<x:String x:Key="AppName">Sirdots</x:String>
</Page.Resources>
<!--
This grid acts as a root panel for the page that defines two rows:
* Row 0 contains the back button and page title
* Row 1 contains the rest of the page layout
-->
<Grid Name="MySampleGrid">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"></ColumnDefinition>
<ColumnDefinition Width="30"></ColumnDefinition>
<ColumnDefinition Width="30"></ColumnDefinition>
<ColumnDefinition Width="30"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Foreground="white" FontSize="40" Height="40">Item 1</TextBlock>
<RadioButton Grid.Row="0" Grid.Column="1" Height="40" GroupName="grpItem1" />
<RadioButton Grid.Row="0" Grid.Column="2" Height="40" GroupName="grpItem1"/>
<RadioButton Grid.Row="0" Grid.Column="3" Height="40" GroupName="grpItem1" />
<TextBlock Grid.Row="1" Grid.Column="0" Foreground="white" FontSize="40" Height="40">Item 2</TextBlock>
<RadioButton Grid.Row="1" Grid.Column="1" Height="40" GroupName="grpItem2"/>
<RadioButton Grid.Row="1" Grid.Column="2" Height="40" GroupName="grpItem2"/>
<RadioButton Grid.Row="1" Grid.Column="3" Height="40" GroupName="grpItem2"/>
<TextBlock Grid.Row="2" Grid.Column="0" Foreground="white" FontSize="40" Height="40">Item 3</TextBlock>
<RadioButton Grid.Row="2" Grid.Column="1" Height="40" GroupName="grpItem3"/>
<RadioButton Grid.Row="2" Grid.Column="2" Height="40" GroupName="grpItem3" />
<RadioButton Grid.Row="2" Grid.Column="3" Height="40" GroupName="grpItem3" />
<Button Grid.Row="3" Grid.Column="0" Content="Click Me" Click="Button_Click"></Button>
</Grid>
</Page>
锡尔多茨
项目1
项目2
项目3
这是我的按钮的点击事件,它只能对一个groupName执行此操作
private void Button_Click(object sender, RoutedEventArgs e)
{
var checkedValue =
MySampleGrid.Children.OfType<RadioButton>()
.FirstOrDefault(r => r.IsChecked.HasValue && r.IsChecked.Value);
}
private void按钮\u单击(对象发送者,路由目标)
{
var校验值=
MySampleGrid.Children.OfType()的
.FirstOrDefault(r=>r.IsChecked.HasValue&&r.IsChecked.Value);
}
如何获取选中的值(True或false)和单选按钮的名称,然后写入数据库?使用LINQ的GroupBy按GroupName对它们进行分组,然后在每个分组上使用Select创建一个字典,其中的键是GroupName,值是组中的选定项(使用FirstOrDefault调用).你能展示一些代码示例吗?类似于
MySampleGrid.Children.OfType().GroupBy(radio=>radio.GroupName)。选择(group=>group.FirstOrDefault(button=>button.IsChecked.HasValue&&button.IsChecked.Value))代码>。这将使单选按钮可枚举。然后,您可以直接使用它们的GroupName为每个组获取一个值,或者通过ToDictionary方法进一步处理列表。