【问题标题】:WPF C# - problem with selecting options in DataGridComboBoxColumn controlsWPF C# - 在 DataGridComboBoxColumn 控件中选择选项的问题
【发布时间】:2021-05-05 17:45:20
【问题描述】:

我这几天一直在学习 WPF 技术。我在处理 DataGridComboBoxColumn 控件时遇到问题。一切正常,但是从选择列表中选择一个选项并移动到另一行后,选择值的单元格有一个红色边框,整个控件锁定,没有进一步交互的可能性。 请帮忙。

<DataGridComboBoxColumn x:Name="colCategory"
                                        Header="Category" 
                                        IsReadOnly="False"
                                        Width="Auto" 
                                        MinWidth="150" 
                                        SelectedValueBinding="{Binding Category}" SelectedValuePath="Key"
                                        SelectedItemBinding="{Binding Category}" DisplayMemberPath="Key"
                                     >

还有我的伪代码:

public class PairCategory
{
    public String Key { get; set; }
    public long ? Id { get; set; }
}

public class CategoryDescription : ObservableBase { public PairCategory Category { get => category; set => category = value; } }  

((DataGridComboBoxColumn)colCB.colObj).ItemsSource = new ObservableCollection<PairCategory>(){ new PairCategory(..), .., PairCategory(..) }; this.controlDataGrid.ItemsSource = lstCategory;

【问题讨论】:

    标签: c# wpf combobox binding datagrid


    【解决方案1】:

    我也遇到了数据网格中的组合框的问题。可能为绑定而锁定。我对组合框使用了以下方法

    public void TestScenario()
    {
         var exampleDataList = new List<ExampleData>();
    
         exampleDataList.Add(new ExampleData("UK"));
         exampleDataList.Add(new ExampleData("USA"));
         exampleDataList.Add(new ExampleData("France"));
         ExampleDataGrid.ItemsSource = exampleDataList;
    }
    
    
    
    
    public class ExampleData : INotifyPropertyChanged
    {
       public event PropertyChangedEventHandler PropertyChanged;
    
       public ObservableCollection<string> countries { get; set; }
       public ObservableCollection<string> cities { get; set; }
    
       private string country;
       private string city;
    
       public string Country
       {
           get
           {
              return country;
           }
           set
           {
               country = value;
               // Call OnPropertyChanged whenever the property is updated
               OnPropertyChanged("Country");
           }
      }
      public string City
      {
          get
          {
              return city;
          }
          set
          {
              city = value;
          }
      }
          
    
      public ExampleData(string country)
      {
         this.country = country;
         countries = new ObservableCollection<string>() { "UK", "USA", "France" };
         cities = new ObservableCollection<string>() { "London", "Bristol", "Plymouth" };
      }
    
      // Create the OnPropertyChanged method to raise the event 
      protected void OnPropertyChanged(string name)
      {
          UpdateOptions();
    
         PropertyChangedEventHandler handler = PropertyChanged;
         if (handler != null)
         {
              handler(this, new PropertyChangedEventArgs(name));
    
         }
      }
    
      public void UpdateOptions()
      {
          List<string> oldCities = cities.ToList<string>();
          List<string> newCities;
    
          foreach (string city in oldCities)
          {
              cities.Remove(city);
          }
    
          if (country == "UK")
          {
               newCities = new List<string>() { "London", "Bristol", "Birmingham" };
          }
          else if (country == "USA")
          {
               newCities = new List<string>() { "New York", "LA", "Texas" };
          }
          else if (country == "France")
          {
               newCities = new List<string>() { "Paris", "Lyon", "Nice" };
          }
          else
          {
              newCities = new List<string>();
          }
    
          foreach (string city in newCities)
          {
              cities.Add(city);
          }
       }
    }
    

    xml 代码

       <DataGrid
        Name="ExampleDataGrid"
        AutoGenerateColumns="False"
        >
                <DataGrid.Columns>
    
                    <DataGridTemplateColumn Header="Country">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox ItemsSource="{Binding countries}" SelectedItem="{Binding Country, UpdateSourceTrigger=PropertyChanged}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
    
                    <DataGridTemplateColumn Header="city">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox ItemsSource="{Binding cities}" SelectedItem="{Binding City, UpdateSourceTrigger=PropertyChanged}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
    
                </DataGrid.Columns>
            </DataGrid>
    

    【讨论】:

      猜你喜欢
      • 2011-12-25
      • 2013-11-20
      • 2010-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 2010-09-12
      相关资源
      最近更新 更多