我是WPF的新手,有一个显然非常简单或基本的问题,尽管我已经阅读了几个小时来找到解决方案,但我还是没有解决。下面是:我有两个组合框,一个主组合框和一个细节组合框。母版有两个条目:"选项1"one_answers"选项2"。详细信息组合框应该有两个条目"Value 1"one_answers"值2"选项1"被选中或值A"and "Value "选项2"在主组合框中被选中。我认为纯XAML应该是可能的。
基本上,我尝试为每个组合框分配一个字典集合,并尝试通过"键"引用(绑定)主列表。以下是到目前为止我的xhtml代码(我已经撤回了所有绑定尝试)
<Window x:Class="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:Test"
mc:Ignorable="d"
xmlns:col="clr-namespace:System.Collections;assembly=mscorlib"
Title="MainWindow" Height="221" Width="457">
<Window.Resources>
<col:ArrayList x:Key="masterlist">
<col:DictionaryEntry Key="option1" Value="Option one"/>
<col:DictionaryEntry Key="option2" Value="Option two" />
</col:ArrayList>
<col:ArrayList x:Key="detaillist">
<col:DictionaryEntry Key="option1" Value="Value 1"/>
<col:DictionaryEntry Key="option1" Value="Value 2"/>
<col:DictionaryEntry Key="option2" Value="Value A"/>
<col:DictionaryEntry Key="option2" Value="Value B"/>
</col:ArrayList>
</Window.Resources>
<Grid Margin="0,0,0,29">
<ComboBox x:Name="ComboboxMaster" HorizontalAlignment="Left" Margin="33,39,0,0" VerticalAlignment="Top" Width="120"
ItemsSource ="{StaticResource masterlist}"
DisplayMemberPath="Value"
SelectedValuePath="Key"
IsSynchronizedWithCurrentItem="True"
SelectedIndex="0">
</ComboBox>
<ComboBox x:Name="ComboboxDetail" HorizontalAlignment="Center" Margin="0,39,0,0" VerticalAlignment="Top" Width="120"
ItemsSource ="{StaticResource detaillist}"
DisplayMemberPath="Value"
SelectedValuePath="Key"
SelectedIndex="0">
</ComboBox>
<Label x:Name="labelMasterKey" HorizontalAlignment="Left" Margin="33,75,0,0" VerticalAlignment="Top" Width="120"
Content="{Binding ElementName=ComboboxMaster, Path=SelectedValue}"/>
<Button Content="Close" HorizontalAlignment="Left" Margin="33,123,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.071,-0.349" Click="Button_Click" Height="26" Width="64"/>
</Grid>
</Window>
感谢你对我这样一个傻瓜的帮助
将详细列表拆分为两个列表,并在主组合框的SelectedValue的DataTrigger的Setter中分配适当的一个:
<Window.Resources>
<col:ArrayList x:Key="masterlist">
<col:DictionaryEntry Key="option1" Value="Option one"/>
<col:DictionaryEntry Key="option2" Value="Option two" />
</col:ArrayList>
<col:ArrayList x:Key="detaillist1">
<col:DictionaryEntry Key="option1" Value="Value 1"/>
<col:DictionaryEntry Key="option2" Value="Value 2"/>
</col:ArrayList>
<col:ArrayList x:Key="detaillist2">
<col:DictionaryEntry Key="option1" Value="Value A"/>
<col:DictionaryEntry Key="option2" Value="Value B"/>
</col:ArrayList>
</Window.Resources>
<StackPanel Orientation="Horizontal" VerticalAlignment="Top">
<ComboBox x:Name="ComboboxMaster" Grid.Column="0" Width="120" Margin="20"
ItemsSource="{StaticResource masterlist}"
DisplayMemberPath="Value"
SelectedValuePath="Key"
SelectedIndex="0"/>
<ComboBox x:Name="ComboboxDetail" Grid.Column="1" Width="120" Margin="20"
DisplayMemberPath="Value"
SelectedValuePath="Key">
<ComboBox.Style>
<Style TargetType="ComboBox">
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedValue,
ElementName=ComboboxMaster}"
Value="option1">
<Setter Property="ItemsSource"
Value="{StaticResource detaillist1}"/>
<Setter Property="SelectedValue"
Value="option1"/>
</DataTrigger>
<DataTrigger Binding="{Binding SelectedValue,
ElementName=ComboboxMaster}"
Value="option2">
<Setter Property="ItemsSource"
Value="{StaticResource detaillist2}"/>
<Setter Property="SelectedValue"
Value="option1"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
</ComboBox>
</StackPanel>
另一种可能是使用MVVM模式。这为您的数据提供了更大的灵活性,并将数据从视图中分离出来。
首先你必须像这样创建一个新的ViewModel类:
public class MainWindowViewModel : INotifyPropertyChanged
{
private Dictionary<string, List<string>> _comboboxData;
private string _selectedItem1;
private string _selectedItem2;
public event PropertyChangedEventHandler PropertyChanged;
public MainWindowViewModel()
{
_comboboxData = new Dictionary<string, List<string>>
{
{"option1", new List<string>{"Value 1", "Value 2"}},
{"option2", new List<string>{"Value A", "Value B"}},
};
_selectedItem1 = "option1";
_selectedItem2 = _comboboxData[SelectedItem1][0];
}
public string SelectedItem1
{
get => _selectedItem1;
set
{
if (value == _selectedItem1) return;
_selectedItem1 = value;
RaisePropertyChanged();
RaisePropertyChanged(nameof(Combobox2Data));
SelectedItem2 = _comboboxData[SelectedItem1][0]
}
}
public string SelectedItem2
{
get => _selectedItem2;
set
{
if (value == _selectedItem2) return;
_selectedItem2 = value;
RaisePropertyChanged();
}
}
public ObservableCollection<string> Combobox1Data => new ObservableCollection<string>(_comboboxData.Keys);
public ObservableCollection<string> Combobox2Data =>
new ObservableCollection<string>(_comboboxData[_selectedItem1]);
private void RaisePropertyChanged([CallerMemberName]string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
使用这个WPF主窗口。Xaml看起来像这样:
<Window x:Class="WpfTest.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:WpfTest"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<local:MainWindowViewModel/>
</Window.DataContext>
<Grid Margin="0,0,0,29">
<ComboBox x:Name="ComboboxMaster" HorizontalAlignment="Left" Margin="33,39,0,0" VerticalAlignment="Top" Width="120"
ItemsSource ="{Binding Combobox1Data}"
SelectedItem="{Binding SelectedItem1}">
</ComboBox>
<ComboBox x:Name="ComboboxDetail" HorizontalAlignment="Center" Margin="0,39,0,0" VerticalAlignment="Top" Width="120"
ItemsSource="{Binding Combobox2Data}"
SelectedItem="{Binding SelectedItem2}">
</ComboBox>
<Label x:Name="labelMasterKey" HorizontalAlignment="Left" Margin="33,75,0,0" VerticalAlignment="Top" Width="120"
Content="{Binding SelectedItem2}"/>
<Button Content="Close" HorizontalAlignment="Left" Margin="33,123,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.071,-0.349" Height="26" Width="64"/>
</Grid>
</Window>
有了这个,你可以用更多的值来改变你的数据字典,例如,对于ComboBox 1(键)或更多的值为每个键列表(值)只在ViewModel类的开始,而不需要在任何地方进行其他更改
«Pure»XAML:
<Window.Resources>
<col:ArrayList x:Key="masterlist">
<col:DictionaryEntry Key="Option one">
<col:DictionaryEntry.Value>
<col:ArrayList>
<col:DictionaryEntry Key="option1" Value="Value 1"/>
<col:DictionaryEntry Key="option1" Value="Value 2"/>
</col:ArrayList>
</col:DictionaryEntry.Value>
</col:DictionaryEntry>
<col:DictionaryEntry Key="Option one">
<col:DictionaryEntry.Value>
<col:ArrayList>
<col:DictionaryEntry Key="option2" Value="Value A"/>
<col:DictionaryEntry Key="option2" Value="Value B"/>
</col:ArrayList>
</col:DictionaryEntry.Value>
</col:DictionaryEntry>
</col:ArrayList>
</Window.Resources>
<Grid Margin="0,0,0,29">
<ComboBox x:Name="ComboboxMaster" HorizontalAlignment="Left" Margin="33,39,0,0" VerticalAlignment="Top" Width="120"
ItemsSource ="{StaticResource masterlist}"
DisplayMemberPath="Key"
SelectedIndex="0">
</ComboBox>
<ComboBox x:Name="ComboboxDetail" HorizontalAlignment="Center" Margin="0,39,0,0" VerticalAlignment="Top" Width="120"
ItemsSource ="{Binding SelectedItem.Value, ElementName=ComboboxMaster}"
DisplayMemberPath="Value"
SelectedIndex="0">
</ComboBox>
<Label x:Name="labelMasterKey" HorizontalAlignment="Left" Margin="33,75,0,0" VerticalAlignment="Top" Width="120"
Content="{Binding ElementName=ComboboxDetail, Path=SelectedItem.Value}"/>