数据网格wpf中的数据绑定组合框 [英] databinding combobox in datagrid wpf
问题描述
亲爱的朋友
我已阅读 一个非常好的样本(https://social.technet.microsoft.com/wiki/contents/articles/26347.wpf-combobox-binding-in-datagrid.aspx)来自
最好的问候,
Cherry
Dear Friends
I have read a very good sample about this in (https://social.technet.microsoft.com/wiki/contents/articles/26347.wpf-combobox-binding-in-datagrid.aspx) from Andy ONeill (very genius) but I cant make it work
I want when user changes the name of the doctor in another combobox family change accordingly
it would be very appreciated if you can help me with
BR
here is my copy from andy's sample
code behind
namespace WpfApp25
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
///
public class NameandFamily : ObservableCollection<DocInfo>
{
}
public class DocInfo
{
public string familyDoc { get; set; }
public string nameDoc { get; set; }
public int id { get; set; }
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainVModel();
}
public class MainVModel : NotifyUIBase
{
public ObservableCollection<DocInfo> docnames { get; set; }
public MainVModel()
{
docnames = new ObservableCollection<DocInfo>();
docnames.Add(new DocInfo { nameDoc = "ahmad", familyDoc = "ahmadi" , id=1 });
docnames.Add(new DocInfo { nameDoc = "javad", familyDoc = "javadi" , id=2});
NameandFamily cc = Application.Current.Resources["ColourSource"] as NameandFamily;
cc.Add(new DocInfo { nameDoc = "ahmad", familyDoc="ahmadi", id=1});
cc.Add(new DocInfo { nameDoc = "ali", familyDoc = "johari" , id=2});
Application.Current.Resources["ColourSource"] = cc;
RaisePropertyChanged("nameDoc");
RaisePropertyChanged("familyDoc");
RaisePropertyChanged("id");
}
}
public class NotifyUIBase : INotifyPropertyChanged
{
// Very minimal implementation of INotifyPropertyChanged matching msdn
// Note that this is dependent on .net 4.5+ because of CallerMemberName
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
}
Xaml
defined resource
<Application.Resources><local:NameandFamily x:Key="ColourSource">
</local:NameandFamily>
</Application.Resources>
bindings
<Window x:Class="WpfApp25.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:WpfApp25"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<DataGrid Margin="48,95,29,47" ItemsSource="{Binding docnames}" AutoGenerateColumns="False" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="لیست نام">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox
ItemsSource="{Binding DataContext.docnames, RelativeSource={RelativeSource AncestorType=local:MainWindow}}"
SelectedValue="{Binding id, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
DisplayMemberPath="nameDoc"
>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridComboBoxColumn
Header="ComboboxColumn"
ItemsSource="{DynamicResource ColourSource}"
SelectedValueBinding="{Binding id, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
SelectedValuePath="id"
DisplayMemberPath="familyDoc"
>
</DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>
<TextBox x:Name="txtName" HorizontalAlignment="Left" Height="23" Margin="48,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="txtFamily" HorizontalAlignment="Left" Height="23" Margin="48,34,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="txtID" HorizontalAlignment="Left" Height="23" Margin="48,57,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="173,18,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
<ComboBox x:Name="cmb" HorizontalAlignment="Left" IsEditable="True" Margin="190,58,0,0" VerticalAlignment="Top" Width="114"
ItemsSource="{DynamicResource ColourSource}"
SelectedValue="{Binding docnames, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
SelectedValuePath="nameDoc"
DisplayMemberPath="familyDoc"
>
</ComboBox>
<ComboBox HorizontalAlignment="Left" Margin="309,58,0,0" VerticalAlignment="Top" Width="114" IsEditable="True"
ItemsSource="{Binding DataContext.docnames, RelativeSource={RelativeSource AncestorType=local:MainWindow}}"
SelectedValue="{Binding docnames, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
SelectedValuePath="nameDoc"
DisplayMemberPath="nameDoc"
>
</ComboBox>
</Grid>
</Window>
Hi 3ib,
I do one sample about populating combobox based on another combobox using xaml, please take a look:
xaml:
<StackPanel> <ComboBox Width="300" Margin="0,20,0,20" DisplayMemberPath="Id" ItemsSource="{Binding parents}" SelectedItem="{Binding selectedparent}" /> <ComboBox Width="100" DisplayMemberPath="name" ItemsSource="{Binding childs}" SelectedItem="{Binding selectedchild}" /> </StackPanel>xaml.cs:
/// </summary> public partial class Window2 : Window { public Window2() { InitializeComponent(); this.DataContext = new viewmodel(); } } public class viewmodel:ViewModelBase { private ObservableCollection<parent> _parents; public ObservableCollection<parent> parents { get { return _parents; } set { _parents = value; RaisePropertyChanged("parents"); } } private ObservableCollection<child> _childs; public ObservableCollection<child> childs { get { return _childs; } set { _childs = value; RaisePropertyChanged("childs"); } } private parent _selectedparent; public parent selectedparent { get { return _selectedparent; } set { _selectedparent = value; RaisePropertyChanged("selectedparent"); childs = _selectedparent.childs; RaisePropertyChanged("childs"); } } private child _selectedchild; public child selectedchild { get { return _selectedchild; } set { _selectedchild = value; RaisePropertyChanged("selectedchild"); } } public viewmodel() { parents = new ObservableCollection<parent>() { new parent(){Id=1,FirstChar="a", childs=new ObservableCollection<child>() { new child(){Id=1,name="a1"}, new child(){Id=2,name="a2"}, new child(){Id=3,name="a3"}, new child(){Id=4,name="a4"}, } }, new parent(){Id=2,FirstChar="b", childs=new ObservableCollection<child>() { new child(){Id=1,name="b1"}, new child(){Id=2,name="b2"}, new child(){Id=3,name="b3"}, new child(){Id=4,name="b4"}, } }, new parent(){Id=3,FirstChar="c", childs=new ObservableCollection<child>() { new child(){Id=1,name="c1"}, new child(){Id=2,name="c2"}, new child(){Id=3,name="c3"}, new child(){Id=4,name="c4"}, } } }; selectedparent = parents[0]; } } public class parent { public Int32 Id { get; set; } public string FirstChar { get; set; } public ObservableCollection<child> childs { get; set; } } public class child { public Int32 Id { get; set; } public string name { get; set; } }
Here is also a sample, you can take a look:
Best Regards,
Cherry
这篇关于数据网格wpf中的数据绑定组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文