wpf DataGridComboBoxColumn绑定无效是什么原因?
<Windowx:Class="MyDemo.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml...
<Window x:Class="MyDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyDemo"
Title="MainWindow" Height="350" Width="525" Name="wnd">
<Window.Resources>
</Window.Resources>
<StackPanel>
<DataGrid ItemsSource="{Binding Path=SelectedList,ElementName=wnd}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="text" Binding="{Binding Path=Value}" />
<DataGridComboBoxColumn Header="comb">
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Path=SelectionList,ElementName=wnd}" />
<Setter Property="SelectedValue" Value="{Binding Path=Value}" />
<Setter Property="SelectedValuePath" Value="{Binding Path=Value}" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Path=SelectionList,ElementName=wnd}" />
<Setter Property="SelectedValue" Value="{Binding Path=Value}" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
</DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>
</StackPanel>
</Window>
namespace MyDemo
{
public class DataItem
{
public int Value { get; set; }
public DataItem(int val) { Value = val; }
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
SelectedList.Add(new DataItem(1)); SelectedList.Add(new DataItem(2));
SelectionList.Add(1); SelectionList.Add(2); SelectionList.Add(3);
}
public ObservableCollection<DataItem> SelectedList
{
get { return _selectedList; }
set { _selectedList = value; }
}
private ObservableCollection<DataItem> _selectedList = new ObservableCollection<DataItem>();
public ObservableCollection<int> SelectionList
{
get { return _selectionList; }
set { _selectionList = value; }
}
private ObservableCollection<int> _selectionList = new ObservableCollection<int>();
}
} 展开
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyDemo"
Title="MainWindow" Height="350" Width="525" Name="wnd">
<Window.Resources>
</Window.Resources>
<StackPanel>
<DataGrid ItemsSource="{Binding Path=SelectedList,ElementName=wnd}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="text" Binding="{Binding Path=Value}" />
<DataGridComboBoxColumn Header="comb">
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Path=SelectionList,ElementName=wnd}" />
<Setter Property="SelectedValue" Value="{Binding Path=Value}" />
<Setter Property="SelectedValuePath" Value="{Binding Path=Value}" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Path=SelectionList,ElementName=wnd}" />
<Setter Property="SelectedValue" Value="{Binding Path=Value}" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
</DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>
</StackPanel>
</Window>
namespace MyDemo
{
public class DataItem
{
public int Value { get; set; }
public DataItem(int val) { Value = val; }
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
SelectedList.Add(new DataItem(1)); SelectedList.Add(new DataItem(2));
SelectionList.Add(1); SelectionList.Add(2); SelectionList.Add(3);
}
public ObservableCollection<DataItem> SelectedList
{
get { return _selectedList; }
set { _selectedList = value; }
}
private ObservableCollection<DataItem> _selectedList = new ObservableCollection<DataItem>();
public ObservableCollection<int> SelectionList
{
get { return _selectionList; }
set { _selectionList = value; }
}
private ObservableCollection<int> _selectionList = new ObservableCollection<int>();
}
} 展开
推荐于2016-04-16 · 知道合伙人互联网行家
关注
展开全部
应该是绑定过程出错,可以参考下面正确的步骤:
1、准备数据源。基类为Student,数据对象为Student3,数据集为StuList3。
2、若要填充下拉列表,请首先使用下列选项之一设置 ComboBox 的 ItemsSource 属性。
·静态资源。
·x:Static 代码实体。
·ComboBoxItem 类型的内联集合。
3、使用静态资源做为ItemSource。
4、使用x:Static扩展标记作为ItemSource。首先定义一个静态属性,然后通过x:Static扩展标记指向该静态属性。
5、使用ComboBoxItem 类型的内联集合做为ItemSource。
6、使用普通数据集合作为ItemSource。
由于DataGridComboBoxColumn没有DataContext属性,所以ItemSource直接绑定普通数据集合将不起作用,需要使用编辑样式(EditingElementStyle)和显示样式(ElementStyle)来呈现数据。
展开全部
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询