2016-06-16 21 views
1

これは、WPFとバインディングで作業するのが比較的新しいと言ってこれを前にする必要があります。 ComboBoxItemをDataGridTemplate列のComboBoxにバインドしようとしています。DataGridのComboBoxにComboBoxItemをバインドします。

DataGrid内のComboBoxにコードに追加したオブジェクトが含まれているため、バインディングが実際に動作することがわかります。私が経験している問題は、SelectedItemがオブジェクトのコンストラクタ内で割り当てられているときに表示されていないということです。

私は問題を再現するためにデモアプリケーションをセットアップしました。

namespace ComboBoxBindingTests 
{ 
public partial class MainWindow : Window 
{ 
    public static ObservableCollection<MyClass> myCollection { get; set; } 

    public MainWindow() 
    { 
     InitializeComponent(); 

     myCollection = new ObservableCollection<MyClass>(); 

     MyClass t = new MyClass("arb1"); 
     myCollection.Add(t); 
    } 
} 



public class MyClass 
{ 
    private ObservableCollection<ComboBoxItem> _possibleComboBoxItems; 
    public ObservableCollection<ComboBoxItem> PossibleComboBoxItems 
    { 
     get { return _possibleComboBoxItems; } 
     set 
     { 
      _possibleComboBoxItems = value; 
      OnPropertyChanged("PossibleComboBoxItems"); 
     } 
    } 
    private ObservableCollection<string> _possibleStrings; 
    public ObservableCollection<string> PossibleStrings 
    { 
     get { return _possibleStrings; } 
     set 
     { 
      _possibleStrings = value; 
      OnPropertyChanged("PossibleStrings"); 
     } 
    } 
    private ComboBoxItem _someComboBoxItem; 
    public ComboBoxItem SomeComboBoxItem 
    { 
     get { return _someComboBoxItem; } 
     set 
     { 
      if (value != null) 
      { 
       ComboBoxItem cbxi = (ComboBoxItem)value; 
       _someComboBoxItem = cbxi; 
       OnPropertyChanged("SomeComboBoxItem"); 
      } 
     } 
    } 
    private string _someString; 
    public string SomeString 
    { 
     get { return _someString; } 
     set 
     { 
      if (value.Contains("System.Windows.Controls.ComboBoxItem: ")) 
       _someString = value.Replace("System.Windows.Controls.ComboBoxItem: ", ""); 
      else 
       _someString = value; 

      OnPropertyChanged("SomeString"); 
     } 
    } 

    public MyClass(string chosenString) 
    { 
     PossibleComboBoxItems = new ObservableCollection<ComboBoxItem>(); 
     PossibleComboBoxItems.Add(new ComboBoxItem() { Content = "arb1", Height = 20, IsEnabled = true }); 
     PossibleComboBoxItems.Add(new ComboBoxItem() { Content = "arb2", Height = 20, IsEnabled = true }); 

     PossibleStrings = new ObservableCollection<string>(); 
     PossibleStrings.Add("arb1"); 
     PossibleStrings.Add("arb2"); 

     SomeString = chosenString; 

     if (chosenString != "") 
     { 
      ComboBoxItem cbxi = new ComboBoxItem(); 
      cbxi.Content = chosenString; 
      cbxi.Height = 20; 
      cbxi.IsEnabled = true; 
      SomeComboBoxItem = cbxi;     
     } 

    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    private void OnPropertyChanged(string propertyName) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler == null) return; 
     handler(this, new PropertyChangedEventArgs(propertyName)); 
    }   
} 
} 

私だけではなく、文字列のComboBoxItemを使いたい理由コードビハインド私はコンボボックス内のオブジェクトを表示する必要があるということですXAML

<Window x:Class="ComboBoxBindingTests.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:ComboBoxBindingTests" 
    mc:Ignorable="d" 
    Title="MainWindow" Height="350" Width="500"> 
<Grid> 
    <DataGrid x:Name="dg" ItemsSource="{Binding myCollection}" DataContext="{Binding RelativeSource={RelativeSource AncestorType=Window}}" AutoGenerateColumns="False"> 
     <DataGrid.Columns> 
      <DataGridTemplateColumn Width="200" Header="StringColumn"> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <ComboBox ItemsSource="{Binding PossibleStrings}" SelectedItem="{Binding SomeString, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Height="22" /> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
      </DataGridTemplateColumn> 
      <DataGridTemplateColumn Width="200" Header="ComboBoxItemColumn"> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <ComboBox ItemsSource="{Binding PossibleComboBoxItems}" SelectedItem="{Binding SomeComboBoxItem, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Height="22" /> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
      </DataGridTemplateColumn> 
     </DataGrid.Columns> 
    </DataGrid> 
</Grid> 
</Window> 

ましたユーザーは選択できません。私の実際のソフトウェアには、いくつかのオブジェクトでIsEnabledプロパティをfalseに設定する条件があります。

DataGridの両方の列が "arb1"をSelectedItemとして表示すると予想されましたが、文字列にバインドされている列のみが機能します。 MainWindow showing that only one column displays the SelectedItem

私はかなりの時間の解決策を見つけようとしており、誰かが私を助けることができれば非常に感謝しています。

答えて

0

私は最終的に解決策を見つけました。

ComboBoxItem cbxi = new ComboBoxItem(); 
cbxi.Content = chosenString; 
cbxi.Height = 20; 
cbxi.IsEnabled = true; 
SomeComboBoxItem = cbxi; 

行が

SomeComboBoxItem = PossibleComboBoxItems.SingleOrDefault(x => chosenString == x.Content.ToString()); 
に交換する必要があります
関連する問題