[英]How to display data from datagrid to radio button?
So I'm new to WPF, and I want when I click on the DataGrid the RadioButton will display the same data as in the DataGrid.所以我是 WPF 的新手,我希望当我单击 DataGrid 时,RadioButton 将显示与 DataGrid 中相同的数据。
sorry for my bad English.对不起,我的英语不好。
Code #代码 #
private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
DataGrid datagrid = (DataGrid)sender;
DataRowView row_selected = datagrid.SelectedItem as DataRowView;
if (row_selected != null)
{
Name_txt.Text = row_selected["Name"].ToString();
Age_txt.Text = row_selected["Age"].ToString();
Address_txt.Text = row_selected["Address"].ToString();
if (rbMale.IsChecked == true)
{
row_selected["Gender"].ToString();
}
else
{
row_selected["Gender"].ToString();
}
}
}
Hope this helps.希望这可以帮助。
I hope to get some simple answers as I am new to WPF.我是 WPF 的新手,希望能得到一些简单的答案。
Here is a very simple code:这是一个非常简单的代码:
XAML XAML
<Window x:Class="RadioButtonDataGridTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:RadioButtonDataGridTest"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"
Loaded="OnLoaded">
<Grid>
<StackPanel>
<DataGrid ItemsSource="{Binding People}" AutoGenerateColumns="False" SelectionChanged="DataGrid_SelectionChanged">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />
<DataGridTextColumn Header="Age" Binding="{Binding Age}" />
<DataGridTextColumn Header="Address" Binding="{Binding Address}" />
<DataGridTextColumn Header="Gender" Binding="{Binding Gender}" />
</DataGrid.Columns>
</DataGrid>
<Label FontWeight="Bold" Content="Name" Margin="0,10,0,0"/>
<TextBox x:Name="Name_txt" />
<Label FontWeight="Bold" Margin="0,10,0,0">Male or female?</Label>
<RadioButton GroupName="sex" Name="rbMale">Male</RadioButton>
<RadioButton GroupName="sex" Name="rbFemale">Female</RadioButton>
</StackPanel>
</Grid>
</Window>
Code behind代码隐藏
As you wish, the RadioButton
is selected when the DataGrid
selection change如您所愿,当DataGrid
选择更改时选择RadioButton
public partial class MainWindow : Window
{
private ObservableCollection<Person> people;
public MainWindow()
{
InitializeComponent();
DataContext = this;
people = new ObservableCollection<Person>();
}
public ObservableCollection<Person> People { get { return people; } }
private void OnLoaded(object sender, RoutedEventArgs e)
{
people.Clear();
people.Add(new Person() { Name="John", Age=12, Address="John address", Gender="Male"});
people.Add(new Person() { Name = "Mary", Age = 23, Address = "Mary address", Gender = "Female" });
}
private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Person person = null;
DataGrid datagrid = (DataGrid)sender;
if (datagrid == null || datagrid.SelectedItems.Count != 1) return;
person = datagrid.SelectedItem as Person;
if (person != null)
{
Name_txt.Text = person.Name;
//Age_txt.Text = row_selected["Age"].ToString();
//Address_txt.Text = row_selected["Address"].ToString();
if (person.Gender == "Male") rbMale.IsChecked = true;
else if (person.Gender == "Female") rbFemale.IsChecked = true;
}
}
}
May I suggest you to use Enum or integer (byte) value to manage the gender than compare strings?我可以建议您使用 Enum 或 integer(字节)值来管理性别而不是比较字符串吗?
The model model
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string Address { get; set; }
public string Gender { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.