WPF DataGridComboBoxColumn怎么获取选中值
本帖最后由 大大怪ZIFA 于 2020-9-3 09:17 编辑<DataGridComboBoxColumn Header="特殊要求" x:Name="cbo_Special" Width="2*"/>
xaml中在DataGrid 设置DataGridComboBoxColumn 列,绑定了下拉列表数据源
DataTable dt1 = myClient.Button_Click_SelectSpecial().Tables;
cbo_Special.ItemsSource = dt1.DefaultView;
cbo_Special.DisplayMemberPath = "SpecialName";
cbo_Special.SelectedValuePath = "SpecialID";
这该如何获取下拉列表的选中值,有什么方法吗{:301_985:} 为毛不用数据绑定?
类似这样:
xmal:
<Window x:Class="WpfApp1.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:WpfApp1"
xmlns:core="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<ObjectDataProvider x:Key="SexEnumKey" MethodName="GetValues" ObjectType="{x:Type core:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type Type="local:Sex"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="20"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Button Grid.Row="0"Name="Btn" Content="获取性别" Click="Btn_OnClick"></Button>
<DataGrid Grid.Row="1" Name="Dg" ItemsSource="{Binding}" CanUserAddRows="False" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridComboBoxColumn Header="性别" SelectedItemBinding="{Binding Sex}" ItemsSource="{Binding Source={StaticResource SexEnumKey}}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
后台代码:
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows;
namespace WpfApp1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
/// <summary>
///
/// </summary>
private readonly ObservableCollection<Customer> _data = new ObservableCollection<Customer>();
public MainWindow()
{
InitializeComponent();
_data.Add(new Customer {Sex = Sex.Man});
_data.Add(new Customer {Sex = Sex.Female});
Dg.DataContext = _data;
}
private void Btn_OnClick(object sender, RoutedEventArgs e)
{
var sex = _data.First().Sex.ToString();
MessageBox.Show(sex);
}
}
public class Customer
{
public Sex Sex { get; set; }
}
public enum Sex
{
Man,
Female
};
}
xiao24 发表于 2020-9-3 10:45
为毛不用数据绑定?
类似这样:
那怎么获取选中值呢 大大怪ZIFA 发表于 2020-9-3 11:09
那怎么获取选中值呢
这个数据是双向绑定的,直接从_data里面拿就可以了
页:
[1]