2011-04-14 59 views
6

如果我在Visual Studio 2010中設置了新的WPF應用程序並添加了以下代碼+ XAML,則會打開一個數據網格,其中包含組合框。現在的問題是,通過組合框更改值不會傳播到綁定的數據模型。換句話說:名爲MyValue的屬性永遠不會被設置。現在花了我幾個小時,我不知道爲什麼這不起作用。還有許多類似的線索和建議沒有。DataGrid中的WPF組合框數據綁定/更新不起作用

這裏是XAML。它只是一個包含DataGrid的簡單窗口。 DataGrid具有設置CellTemplate和CellEditingTemplate的模板列。它們都包含一個ComboBox,其中充滿了來自資源部分的列表。 ComboBox.SelectedItem綁定到MyItem.MyValue:

<Window x:Class="DataGridComboBoxExample.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" xmlns:local="clr-namespace:DataGridComboBoxExample"> 

    <Window.Resources> 

     <local:MyItemList x:Key="ItemList"/> 

     <DataTemplate x:Key="NotificationModeDataTemplate"> 
      <ComboBox 
       ItemsSource="{StaticResource ItemList}" 
       SelectedItem="{Binding Path=MyValue, Mode=OneWay}" /> 
     </DataTemplate> 
     <DataTemplate x:Key="NotificationModeEditTemplate"> 
      <ComboBox 
       ItemsSource="{StaticResource ItemList}" 
       SelectedItem="{Binding Path=MyValue, Mode=TwoWay}" /> 
     </DataTemplate> 

    </Window.Resources> 

    <Grid> 
     <DataGrid x:Name="myDataGrid" AutoGenerateColumns="False"> 
      <DataGrid.Columns> 
       <DataGridTemplateColumn 
        Header="Test" Width="100" 
        CellTemplate="{StaticResource NotificationModeDataTemplate}" 
        CellEditingTemplate="{StaticResource NotificationModeEditTemplate}" /> 
      </DataGrid.Columns> 
     </DataGrid> 
    </Grid> 
</Window> 

這裏的代碼。它包含剛建立DataContext的主窗口。 MyItem是支持INotifyPropertyChanged的行的數據模型。 MyItemList是綁定到ComboBox.ItemsSource的選項列表。

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 

     myDataGrid.ItemsSource = new List<MyItem> 
     { 
      new MyItem { MyValue = "i0" }, 
      new MyItem { MyValue = "i1" }, 
      new MyItem { MyValue = "i0" }, 
     }; 
    } 
} 

public class MyItem : INotifyPropertyChanged 
{ 
    public string MyValue 
    { 
     get { return myValue; } 
     set 
     { 
      myValue = value; 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs("MyValue")); 
      } 
     } 
    } 
    private string myValue; 

    public event PropertyChangedEventHandler PropertyChanged; 
} 

public class MyItemList : List<string> 
{ 
    public MyItemList() { Add("i0"); Add("i1"); Add("i2"); } 
} 

回答

10

我懷疑你需要使SelectedItem綁定更新PropertyChanged上的源以使其工作。

的SelectedItem = 「{綁定路徑= myvalue的,模式=雙向,UpdateSourceTrigger =的PropertyChanged}」

我會做的CellTemplate和CellEditingTemplate均引用您的編輯模板,而這調試,以消除其他,無關緊要,模板,直到你把它整理出來。

+1

我剛剛測試過它,它對我有用。我將CellTemplate和CellEditingTemplate都引用編輯模板,並添加了UpdateSourceTrigger = PropertyChanged。當我在組合框中選擇一個項目時,調用MyValue設置器。我正在對安裝VS 2010 SP1的.NET 4.0進行測試。 – 2011-04-14 20:52:52

+0

HEUREKA!現在它可以工作。非常感謝你! – MRoc 2011-04-14 20:53:36

+1

爲什麼這實際上工作?如果我在DataGrid之外移動相同的'ComboBox'(僅僅是一個列表),這對相同的視圖模型不需要。 – 2017-08-22 01:26:03