2017-01-09 74 views
0

我是WPF + C#中的新成員,並開始掌握它。在沒有Somegrid.Items.Refresh()的情況下在XAML中更新ComboBox

我想在程序運行時更新DataGrid中的ComboBox。 不幸的是,如果我使用Somegrid.Item.Refresh()更新DataGrid,ComboBox從不顯示任何選定的項目。


這裏是XAML

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <DataGrid Name="Somegrid" HorizontalAlignment="Left" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False"> 
      <DataGrid.Columns> 

       <DataGridTemplateColumn> 
        <DataGridTemplateColumn.Header> 
         <TextBlock Text="SomebodY" /> 
        </DataGridTemplateColumn.Header> 
        <DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <ComboBox ItemsSource="{Binding Somebody}" SelectionChanged="ComboBox_SelectionChanged_Somebody" /> 
         </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
       </DataGridTemplateColumn> 

       <DataGridTemplateColumn> 
        <DataGridTemplateColumn.Header> 
         <TextBlock Text="SomethinG" /> 
        </DataGridTemplateColumn.Header> 
        <DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <ComboBox ItemsSource="{Binding Something}" SelectionChanged="ComboBox_SelectionChanged_Something" /> 
         </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
       </DataGridTemplateColumn> 

       <DataGridTemplateColumn> 
        <DataGridTemplateColumn.Header> 
         <TextBlock Text="SomewherE" /> 
        </DataGridTemplateColumn.Header> 
        <DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <ComboBox ItemsSource="{Binding Somewhere}" SelectionChanged="ComboBox_SelectionChanged_Somewhere" /> 
         </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
       </DataGridTemplateColumn> 

      </DataGrid.Columns> 
     </DataGrid> 
    </Grid> 
</Window> 

背後代碼

using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.Windows; 

namespace WpfApplication1 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 

     public MainWindow() 
     { 
      InitializeComponent(); 

      someclassFoo.Somebody = new List<string>() { "Alan", "Benjamin" }; 

      observableSomeclassFoo.Add(someclassFoo); 
      Somegrid.ItemsSource = observableSomeclassFoo; 

     } 


     public SomeclassFoo someclassFoo = new SomeclassFoo(); 
     private ObservableCollection<SomeclassFoo> _observableSomeclassFoo = new ObservableCollection<SomeclassFoo>(); 
     public ObservableCollection<SomeclassFoo> observableSomeclassFoo { get { return _observableSomeclassFoo; } } 


     private void ComboBox_SelectionChanged_Somebody(object sender, RoutedEventArgs e) 
     { 
      System.Windows.Controls.ComboBox cb1 = (System.Windows.Controls.ComboBox)sender; 

      if (cb1.SelectedItem.ToString() == "Alan") 
      { 
       someclassFoo.Something = new List<string>() { "Apple" }; 
      } 
      else if (cb1.SelectedItem.ToString() == "Benjamin") 
      { 
       someclassFoo.Something = new List<string>() { "Banana" }; 
      } 

      //Somegrid.Items.Refresh(); 
     } 


     private void ComboBox_SelectionChanged_Something(object sender, RoutedEventArgs e) 
     { 
      System.Windows.Controls.ComboBox cb2 = (System.Windows.Controls.ComboBox)sender; 

      if (cb2.SelectedItem.ToString() == "Apple") 
      { 
       someclassFoo.Somewhere = new List<string>() { "Antartica" }; 
      } 
      else if (cb2.SelectedItem.ToString() == "Banana") 
      { 
       someclassFoo.Somewhere = new List<string>() { "Bedrock" }; 
      } 

      //Somegrid.Items.Refresh(); 
     } 


     private void ComboBox_SelectionChanged_Somewhere(object sender, RoutedEventArgs e) 
     { 
      System.Windows.Controls.ComboBox cb3 = (System.Windows.Controls.ComboBox)sender; 

      MessageBox.Show(cb3.SelectedItem.ToString().ToUpper() + " selected"); 
     } 


    } 
} 

另一類

using System.Collections.Generic; 

namespace WpfApplication1 
{ 
    public class SomeclassFoo 
    { 
     public IList<string> Somebody { get; set; } 

     public IList<string> Something { get; set; } 

     public IList<string> Somewhere { get; set; } 
    } 
} 

如何更新組合框東西和ComboBox 地方不使用Somegrid.Item.Refresh()

回答

1

SomeclassFoo類型應該實現INotifyPropertyChanged接口,提高變更通知:

public class SomeclassFoo : INotifyPropertyChanged 
{ 
    private IList<string> _somebody; 
    public IList<string> Somebody 
    { 
     get { return _somebody; } 
     set { _somebody = value; NotifyPropertyChanged(); } 
    } 

    private IList<string> _something; 
    public IList<string> Something 
    { 
     get { return _something; } 
     set { _something = value; NotifyPropertyChanged(); } 
    } 

    private IList<string> _someWhere; 
    public IList<string> Somewhere 
    { 
     get { return _someWhere; } 
     set { _someWhere = value; NotifyPropertyChanged(); } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    private void NotifyPropertyChanged([CallerMemberName] string propertyName = "") 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

如何實施財產變更通知:https://msdn.microsoft.com/en-us/library/ms743695(v=vs.110).aspx

0

相反刷新電網,去掉電流源,並設置了新的來源:

yourGrid.ItemsSource = null;

yourGrid.ItemsSource = yourDataSource;

相關問題