2012-12-12 3 views
0

DataGridComboxClolumn 요소가 2 개인 DataGrid가 있고 첫 번째 값이 변경되면 두 번째 DataGridComboxClolumn 요소의 ItemsSource를 변경하려고합니다. 그런 바인딩을 어떻게 설정할 수 있습니까?WPF 변경 DataGrid 콤보 상자 ItemsSource 다른 DataGrid의 값으로 combox

참고 : 데이터 그리드는 자신의 itemssource을 가지고 있으며, 데이터가 계층 적 다음과 같다 :

DataGrid를 (목록 연산 형 채널)의

ItemsSource가 :

public class Root 
{ 
    public List<Channel> Ch { get; set; } // This is the ItemsSource of the datagrid itself 
} 


public class Channel 
{ 
    public Settings TheSettings { get; set; } 
} 

내가 달성하고 싶은 일이 있다는 것입니다 설정은 두 번째 ComboBox의 선택된 값으로 설정됩니다. 이 작업은 ComboBox ItemsSource를 설정하여 쉽게 수행 할 수 있습니다. 나는 두 번째 콤보 스의 ItemsSource가 동적이어야한다고 요구하지만. 예 : 그것은 첫 번째 콤보 박스에서 선택된 소스로 변경해야합니다. 어떻게 할 수 있습니까?

답변

1

당신은 당신의 DataGridTemplateColumn이와 콤보를 만들 수 1

옵션. 첫 번째 항목은 주 ViewModel의 항목으로 채울 수 있으며 두 번째 항목은 첫 번째 항목의 SelectedItem 항목에 바인딩됩니다.

<Window x:Class="WpfApplication1.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:i="http://schemas.microsoft.com/expression/2010/interactivity" 
    xmlns:local="clr-namespace:WpfApplication1" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    Title="MainWindow" 
    Width="525" 
    Height="350" 
    mc:Ignorable="d"> 
<Window.DataContext> 
    <local:Root/> 
</Window.DataContext> 
<Grid x:Name="LayoutRoot"> 
    <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Ch}" CanUserAddRows="False"> 
     <DataGrid.Columns> 
      <DataGridTemplateColumn> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <Grid> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition/> 
          <ColumnDefinition/> 
         </Grid.ColumnDefinitions> 
         <ComboBox Width="100" ItemsSource="{Binding DataContext.ComboBox1Items, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" x:Name="ComboBox1"/> 
         <ComboBox Grid.Column="1" Width="100" ItemsSource="{Binding SelectedItem.Items, ElementName=ComboBox1}" SelectedItem="{Binding TheSettings}" IsEditable="True" IsReadOnly="True"/> 
         </Grid> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
      </DataGridTemplateColumn> 
     </DataGrid.Columns> 
    </DataGrid> 

</Grid> 
</Window> 

public class ComboBox1Item 
{ 
    public string Label { get; set; } 
    public List<string> Items { get; set; } 

    public override string ToString() 
    { 
     return this.Label; 
    } 
} 

public class Root 
{ 
    public List<Channel> Ch { get; set; } 

    public List<ComboBox1Item> ComboBox1Items { get; set; } 

    public Root() 
    { 
     this.Ch = new List<Channel>(){ 
      new Channel(){ TheSettings = "Settings1"}, 
      new Channel(){ TheSettings = "Settings2"}, 
     }; 

     this.ComboBox1Items = new List<ComboBox1Item>{ 
      new ComboBox1Item(){ Label = "Item1", 
       Items = new List<string>(){ "Settings1", "Settings2"} 
      }, 
      new ComboBox1Item(){ Label = "Item2", 
       Items = new List<string>(){ "Settings3", "Settings4"} 
      } 
     }; 
    } 
} 

옵션 2

채널 객체를 래핑하는 객체를 생성하고 그 안에 넣어 논리는 하나의 콤보 상자가 다른 항목 구동 할 수 있도록 :

public class ChannelWrapper : INotifyPropertyChanged 
{ 
    #region INotifyPropertyChanged values 

    public event PropertyChangedEventHandler PropertyChanged; 

    protected void OnPropertyChanged(string propertyName) 
    { 
     if (this.PropertyChanged != null) 
     { 
      this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
    #endregion 


    private object comboBox1SelectedItem; 
    public object ComboBox1SelectedItem 
    { 
     get { return this.comboBox1SelectedItem; } 
     set 
     { 
      if (this.comboBox1SelectedItem != value) 
      { 
       this.comboBox1SelectedItem = value; 
       this.OnPropertyChanged("ComboBox1SelectedItem"); 

       // Put the logic to change the items available in the second combobox here 
       if (value == "Value1") 
        this.ComboBox2ItemsSource = new List<object>() { "Setting1", "Setting2" }; 
       if (value == "Value2") 
        this.ComboBox2ItemsSource = new List<object>() { "Setting3", "Setting4" }; 
      } 
     } 
    } 

    private List<object> comboBox2ItemsSource; 
    public List<object> ComboBox2ItemsSource 
    { 
     get { return this.comboBox2ItemsSource; } 
     set 
     { 
      if (this.comboBox2ItemsSource != value) 
      { 
       this.comboBox2ItemsSource = value; 
       this.OnPropertyChanged("ComboBox2ItemsSource"); 
      } 
     } 
    } 

    public Channel Ch { get; set; } 
} 

당신의 그런 다음 루트 클래스는 채널 모음 대신 래퍼 컬렉션을 노출합니다. DataGrid에는 2 개의 ComboBoxColumn이 있습니다. 첫 번째 SelectedItem은 래퍼의 "ComboBox1SelectedItem"속성에 바인딩됩니다. 두 번째 ItemsSource는 래퍼의 "ComboBox2ItemsSource"속성에 바인딩되고 두 번째 열의 SelectedItem은 경로 "Ch.TheSettting"을 사용하여 래퍼의 Channel 인스턴스 설정에 바인딩됩니다.