首页 新闻 会员 周边 捐助

请教 关于WPF中在DataGrid上绑定一个Combox控件的问题

0
悬赏园豆:200 [待解决问题]

先说一下我想实现的效果

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选中的默认项应该第一行是男,第二行是女,

但现在却都是女被选中了。不知道是哪出错了。请教。。

fanl的主页 fanl | 菜鸟二级 | 园豆:202
提问于:2017-12-03 13:48
< >
分享
所有回答(2)
0

vb实在不想看。大概瞄了一下,你好像在设置 值得时候,没有去实现INotifyPropertyChanged的事件,你需要在值改变的是由使用这个接口的事件通知,这样下层界面才知道界面值变了。

花飘水流兮 | 园豆:13595 (专家六级) | 2017-12-03 19:03
0
 <DataGridComboBoxColumn Width="200" Header="性别"  SelectedValueBinding="{Binding Sex, Mode=TwoWay}"  >
                        <DataGridComboBoxColumn.ElementStyle>
                            <Style TargetType="{x:Type ComboBox}">
                                <Setter Property="ItemsSource" Value="{Binding Path=DataContext.SexList, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
                            </Style>
                        </DataGridComboBoxColumn.ElementStyle>
                        <DataGridComboBoxColumn.EditingElementStyle>
                            <Style TargetType="{x:Type ComboBox}">
                                <Setter Property="ItemsSource" Value="{Binding Path=DataContext.SexList, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
                            </Style>
                        </DataGridComboBoxColumn.EditingElementStyle>
                    </DataGridComboBoxColumn>
Yu | 园豆:12990 (专家六级) | 2017-12-05 11:21
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册