WPF Datagrid绑定到Combobox和Textbox

我需要您与WPF Datagrid的帮助。 我在平台上查看了一些回答的问题,并做了类似的事情,但无法解决一些问题。

我有结构命名为表

public struct Table
{
    public string Name { get; set; }
    public int Value { get; set; }
}

和名为Room的Class

public class Room : INotifyPropertyChanged
{
    public string strRoomlName;
    public string Name 
    {
        get { return strRoomlName; }
        set { strRoomlName = value; }
    }               
    public ObservableCollection<Table> tbTables = new ObservableCollection<Table>();
    public ObservableCollection<Table> PrpTables
    {
        get {  return tbTables;  }
        set
        {
            tbTables = value;
            OnPropertyChanged("PrRawValues");
            if (value != null && value.Count > 0)
            {
                PrpSelectedTable = value.First();
            }
        }
    }
    private Table selectedTable;
    public Table PrpSelectedTable
    {
        get
        {
            return selectedTable;
        }
        set
        {
            selectedTable = value;
            OnPropertyChanged("PrpSelectedTable");
            ComboValue = value.Value;
            OnPropertyChanged("ComboValue");     
        }
    }
    public int ComboValue
    {
        get;
        set;
    }        
     public event PropertyChangedEventHandler PropertyChanged;
     protected virtual void OnPropertyChanged(string propertyName)
     {
         PropertyChangedEventHandler handler = PropertyChanged;
         if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
     }     
}

和主类如下,

public partial class MainWindow : Window
{
    ObservableCollection<Room> rooms;
    public MainWindow()
    {
        InitializeComponent();
        rooms = new ObservableCollection<Room>();

        Table t1 = new Table { Name = "Table #1", Value = 0 };
        Table t2 = new Table { Name = "Table #2", Value = 3 };
        Table t3 = new Table { Name = "Table #3", Value = 5 };
        Table t4 = new Table { Name = "Table #4", Value = 2 };

        Room rm1 = new Room();
        rm1.Name = "Small Hall";
        rm1.tbTables.Add(t1);
        rm1.tbTables.Add(t2);
        rm1.tbTables.Add(t3);
        rm1.tbTables.Add(t4);
        rooms.Add(rm1);

        Table t21 = new Table { Name = "Table #1", Value = 0 };
        Table t22 = new Table { Name = "Table #2", Value = 3 };
        Table t23 = new Table { Name = "Table #3", Value = 5 };
        Table t24 = new Table { Name = "Table #4", Value = 12 };
        Table t25 = new Table { Name = "Table #5", Value = 8 };
        Table t26 = new Table { Name = "Table #6", Value = 3 };
        Table t27 = new Table { Name = "Table #7", Value = 20 };


        Room rm2 = new Room();
        rm2.Name = "Main Hall";
        rm2.tbTables.Add(t21);
        rm2.tbTables.Add(t22);
        rm2.tbTables.Add(t23);
        rm2.tbTables.Add(t24);
        rm2.tbTables.Add(t25);
        rm2.tbTables.Add(t26);
        rm2.tbTables.Add(t27);
        rooms.Add(rm2);


        this.gridSignals.DataContext = rooms;
    }

XAML布局:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Custom="http://schemas.microsoft.com/winfx/2006/xaml/presentation/ribbon" x:Class="BindingReiview.MainWindow"
    Title="MainWindow" Height="320" Width="396">
<StackPanel Height="266" VerticalAlignment="Top" Margin="0,0,2,0">
    <DataGrid ItemsSource="{Binding}" Name="gridSignals" CanUserAddRows="False" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header=" Room " Binding="{Binding Name}" Width="110"/>
            <DataGridTemplateColumn Header="   Tables " Width="100" >
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox ItemsSource="{Binding PrpTables, Mode=TwoWay}" SelectedIndex="0" SelectedItem="{Binding PrpSelectedTable, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                            DisplayMemberPath="Name" Name="cmbVal" ></ComboBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="  Geust(s)" Width="100">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding ComboValue}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>               
        </DataGrid.Columns>
    </DataGrid>  
</StackPanel>

ObservableCollection绑定到网格的DataSource。 Combobox项目绑定到Room,其中有ObservableCollection。 选择“表格”时,“宾客人数”显示为“文本框”。 由于文本框文本绑定到ComboValue(Combobox.SelectedItem)。

所以,我需要改变选定的表格的客人。 例如(图中),表#5中的值为8,需要通过编辑文本框来更改。

欢迎任何帮助和建议!


您是否正在更新所选表格(代码/示例图片中的变量t25中的变量属性Value中的Value ? 在这种情况下,您可以直接绑定到该值。

{Binding PrpSelectedTable.Value, Mode=TwoWay}

这将更新所选表格中的值。 我也建议让Table实现INotifyPropertyChanged接口。 这样,如果表格的值或名称属性发生变化,显示器将自动更新。 希望有帮助。

链接地址: http://www.djcxy.com/p/75239.html

上一篇: WPF Datagrid Binding to Combobox and Textbox

下一篇: Live search in a DataTable