【问题标题】:ObservableCollection changing a value assigns a different value on same DataGrid rowObservableCollection 更改值会在同一 DataGrid 行上分配不同的值
【发布时间】:2014-04-14 08:35:51
【问题描述】:

我想知道当来自组合框的 SelectionChanged 事件被引发时,是否可以为数据网格列分配一个值?

例如,

在 DataGrid 内的 ComboBox 中,我有 2 个值“0”和“1”

如果用户选择“0” - 网格内和同一行中名为 ID 的另一列将显示“0”,然后如果用户决定选择“1”,则同一行中的 ID 将更改为“1” "

另一件事是我在 ObservableCollection 中已经有了这些数据,所以基本上如果组合为 0,那么我需要从 ObservableCollection 中获取 0 的 ID。

我希望这是可以理解的。

----- 代码------

        <DataGrid.Columns>
            <DataGridComboBoxColumn SelectedValueBinding="{Binding CID}" SelectedValuePath="CID"  Header="CID" Width="70">
                <DataGridComboBoxColumn.EditingElementStyle>
                    <Style TargetType="ComboBox">

                        <!--<EventSetter Event="SelectionChanged" Handler="abc"></EventSetter>-->


                        <Setter Property="ItemsSource"
                Value="{Binding DataContext.EntityCollection, 
                          RelativeSource={RelativeSource Mode=FindAncestor, 
                                                    AncestorType=Window}}"/>
                        <Setter Property="DisplayMemberPath" Value="CID"/>

                    </Style>
                </DataGridComboBoxColumn.EditingElementStyle>
                <DataGridComboBoxColumn.ElementStyle>
                    <Style TargetType="ComboBox">
                        <Setter Property="ItemsSource"
                Value="{Binding DataContext.EntityCollection,
                          RelativeSource={RelativeSource Mode=FindAncestor, 
                                                     AncestorType=Window}}"/>
                        <Setter Property="DisplayMemberPath" Value="CID"/>
                    </Style>
                </DataGridComboBoxColumn.ElementStyle>

                <DataGridComboBoxColumn.HeaderStyle>
                    <Style TargetType="{x:Type DataGridColumnHeader}">
                        <Setter Property="HorizontalContentAlignment"
                            Value="Center" />
                    </Style>
                </DataGridComboBoxColumn.HeaderStyle>
            </DataGridComboBoxColumn>

              <DataGridTextColumn Header="UID" Binding="{Binding EntityCollection.UID}" Width="70">
            </DataGridTextColumn>

所以基本上,我希望将“UID”TextColumn 更新为用户在 CID 组合框中选择的集合中的任何 ID。

干杯

【问题讨论】:

  • 向我们展示代码。到目前为止你做了什么?
  • 希望这更有意义?我主要是在 XAML 中尝试这样做,所以没有太多 C# 代码完成

标签: c# wpf xaml datagrid observablecollection


【解决方案1】:

将 ComboBox 的 SelectedItem 属性设置为与名为 SelectedItem 的成员绑定:

<DataGridComboBoxColumn SelectedValueBinding="{Binding CID}" SelectedItem="{Binding SelectedItem}" SelectedValuePath="CID"  Header="CID" Width="70">

然后设置一个名为SelectedItem的成员,像这样:

private int _selectedItem;
public int SelectedItem
{
   get { return _selectedItem; }
   set
      {
         _selectedItem = value;
         OnPropertyChanged("SelectedItem");
      }
}

然后在您的 TextBox 中,将绑定设置为 SelectedItem:

 <DataGridTextColumn Header="UID" Binding="{Binding SelectedItem.UID}" Width="70">
            </DataGridTextColumn>

【讨论】:

    【解决方案2】:

    将集合绑定到 Datagrid 组合框列,并使用组合框的选定项绑定另一个数据网格文本列。

    XAML

    <Window x:Class="ComboBoxDGWPF.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="ComboBox DataGrid WPF" Height="300" Width="616"
            xmlns:staticData="clr-namespace:ComboBoxDGWPF"
            xmlns:controls="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit">
    
    <Window.Resources>
        <staticData:StatusList x:Key="StatusList"/>
    </Window.Resources>
    
    <Grid>
        <controls:DataGrid x:Name="dgData" AutoGenerateColumns="False">
            <controls:DataGrid.Columns>
                <controls:DataGridTemplateColumn Header="Status" Width="100">
                    <controls:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Status}"/>
                        </DataTemplate>
                    </controls:DataGridTemplateColumn.CellTemplate>
                    <controls:DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <ComboBox Height="22" ItemsSource="{StaticResource StatusList}" SelectedItem="{Binding Status}"/>
                        </DataTemplate>
                    </controls:DataGridTemplateColumn.CellEditingTemplate>
                </controls:DataGridTemplateColumn>
                <controls:DataGridTextColumn Binding="{Binding Status}" Header="Selected Status" Width="100"/>
            </controls:DataGrid.Columns>
        </controls:DataGrid>
    </Grid>
    </Window>
    

    代码隐藏

    using System.Collections.Generic;
    using System.Windows;
    using System.Windows.Documents;
    
    namespace ComboBoxDGWPF
    {
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
    
                List<TicketInfo> ticketsList = new List<TicketInfo> 
                {
                    new TicketInfo{ Status="Open"},
                    new TicketInfo{ Status="Assigned"},
                    new TicketInfo{ Status="Open"},
                    new TicketInfo{ Status="Open"},
                    new TicketInfo{ Status="Closed"},
                    new TicketInfo{ Status="Open"},
                    new TicketInfo{ Status="Open"}
                };
                dgData.ItemsSource = ticketsList;
            }
        }
    
        public class TicketInfo
        {
            public string Status { get; set; }
        }
    
        public class StatusList : List<string>
        {
            public StatusList()
            {
                this.Add("Assigned");
                this.Add("Closed");
                this.Add("In Progress");
                this.Add("Open");
                this.Add("Resolved");
            }
        }
    }
    

    类似文章发布Here

    【讨论】:

      猜你喜欢
      • 2019-11-10
      • 2011-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多