당신은 당신의 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 인스턴스 설정에 바인딩됩니다.