先说一下我想实现的效果
DataGrid上有两列,第一列是 性别 ,第二列是性别List,这列是Combox。
DataGrid的数据源是DataTable,里面只有ID,Sex两列。
当画面启动后,DataGrid第二列显示默认的性别 是根据第一列性别一致。
我尝试做了个小例子,但combox好像没跟第一例的值关联上。
高手帮看看问题出在哪,谢啦。。
<Window 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:WpfApp2" x:Class="MainWindow" mc:Ignorable="d" Title="MainWindow" Height="228" Width="677"> <Window.DataContext> <local:MainMV/> </Window.DataContext> <Window.Resources> </Window.Resources> <Grid> <StackPanel Margin="10,50,0,0"> <DataGrid x:Name="dataGridBudgetEntries" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" ItemsSource="{Binding Dt}"> <DataGrid.Columns> <DataGridTextColumn Binding="{Binding ID}" Header="ID" IsReadOnly="True"/> <DataGridTextColumn Binding="{Binding Sex}" Header="Sex" IsReadOnly="True"/> <DataGridTemplateColumn Header="性别"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <ComboBox ItemsSource="{Binding Path=DataContext.SexList, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" DisplayMemberPath="Value" SelectedValue="{Binding Dt.Sex, Mode=TwoWay}" SelectedValuePath="Sex" SelectedItem="{Binding Dt.Sex, Mode=TwoWay}" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid> </StackPanel> </Grid> </Window>
ViewModel
Imports System.Data Imports System.ComponentModel Imports System.Collections.ObjectModel Public Class MainMV Implements INotifyPropertyChanged Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged Private Sub NotifyPropertyChanged(Optional ByVal propertyName As String = Nothing) RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName)) End Sub Private _dt As New DataTable Public Property Dt As DataTable Get Return _dt End Get Set(value As DataTable) _dt = value End Set End Property Private _list As New Dictionary(Of String, String) Public Property SexList As Dictionary(Of String, String) Get Return _list End Get Set(value As Dictionary(Of String, String)) _list = value End Set End Property Private Function CreateData() As DataTable Dim dt As New DataTable Dim id As New DataColumn("ID") Dim Sex As New DataColumn("Sex") dt.Columns.Add(id) dt.Columns.Add(Sex) Dim dr As DataRow = dt.NewRow dr.Item("ID") = "101" dr.Item("Sex") = "1" dt.Rows.Add(dr) dr = dt.NewRow dr.Item("ID") = "102" dr.Item("Sex") = "0" dt.Rows.Add(dr) Return dt End Function Private Function CreateList() As Dictionary(Of String, String) Dim lt As New Dictionary(Of String, String) lt.Add("0", "女") lt.Add("1", "男") Return lt End Function Sub New() SexList = CreateList() Dt = CreateData() End Sub End Class
正常结果combox选中的默认项应该第一行是男,第二行是女,
但现在却都是女被选中了。不知道是哪出错了。请教。。
SelectedItem和SelectedValue 去掉
只保留一个SelectedItem就行了吧
wpf combox 里面怎么放入 button 控件啊?