根据另一个控件的状态以 XAML 样式填充 WPF 组合框项目
Populate WPF combo box items in XAML style based on the state of another control
我想我很接近,但我在这里遗漏了一些东西。
我真的在尝试扩展我的 XAML 知识并整理我的 WPF 项目中的代码。
我有一种情况,我想根据另一个控件的状态填充组合框的项目。
我有一种检查复选框值的样式:
<Style
x:Key="{x:Type ComboBox}"
TargetType="{x:Type ComboBox}">
<Style.Triggers>
<DataTrigger
Binding="{Binding ElementName=SomeCheckBox, Path=IsChecked}"
Value="True">
这似乎允许我触发框的值。
我现在正在尝试弄清楚如何设置我的 setter。
似乎无法访问项目 属性 ...
这是我一直在搞砸的事情:
<Setter
Property="Items">
<Setter.Value>
<ComboBoxItem Content="SomeValue" />
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
但很明显这是行不通的。
有没有办法使用 setter?
我是否需要提出某种列表或我存放在 window 资源中并用作项目来源的列表?
我将如何制作该列表?
based on the state of another control
我会在将复选框 IsChecked
绑定到 属性 之后在 VM 后面的代码中执行此操作,同时还将 ComboBox 绑定到一个列表,该列表根据 [= 的值而变化11=]。
这是一个简单的例子:
public bool IsOperationChecked // Bound to the checkbox
{
get { return _IsOperationChecked; }
set {
_IsOperationChecked= value;
OnPropertyChanged("IsOperationChecked");
Names = (value) ? new List<string>() {"alpha", "beta"} :
new List<string>() {"Bill", "Frank"};
}
}
public List<string> Names // Bound to the Combobox
{
get { return _Names; }
set { _Names = value; OnPropertyChanged("Names"); }
}
使用资源中声明的数组:
<Window.Resources>
<x:Array Type="sys:String"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
x:Key="MyArray">
<sys:String>One</sys:String>
<sys:String>Two</sys:String>
</x:Array>
</Window.Resources>
...
<Style x:Key="{x:Type ComboBox}"
TargetType="{x:Type ComboBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=SomeCheckBox, Path=IsChecked}" Value="True">
<Setter Property="ItemsSource" Value="{StaticResource MyArray}" />
</DataTrigger>
</Style.Triggers>
</Style>
我想我很接近,但我在这里遗漏了一些东西。
我真的在尝试扩展我的 XAML 知识并整理我的 WPF 项目中的代码。
我有一种情况,我想根据另一个控件的状态填充组合框的项目。
我有一种检查复选框值的样式:
<Style
x:Key="{x:Type ComboBox}"
TargetType="{x:Type ComboBox}">
<Style.Triggers>
<DataTrigger
Binding="{Binding ElementName=SomeCheckBox, Path=IsChecked}"
Value="True">
这似乎允许我触发框的值。
我现在正在尝试弄清楚如何设置我的 setter。
似乎无法访问项目 属性 ...
这是我一直在搞砸的事情:
<Setter
Property="Items">
<Setter.Value>
<ComboBoxItem Content="SomeValue" />
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
但很明显这是行不通的。
有没有办法使用 setter?
我是否需要提出某种列表或我存放在 window 资源中并用作项目来源的列表?
我将如何制作该列表?
based on the state of another control
我会在将复选框 IsChecked
绑定到 属性 之后在 VM 后面的代码中执行此操作,同时还将 ComboBox 绑定到一个列表,该列表根据 [= 的值而变化11=]。
这是一个简单的例子:
public bool IsOperationChecked // Bound to the checkbox
{
get { return _IsOperationChecked; }
set {
_IsOperationChecked= value;
OnPropertyChanged("IsOperationChecked");
Names = (value) ? new List<string>() {"alpha", "beta"} :
new List<string>() {"Bill", "Frank"};
}
}
public List<string> Names // Bound to the Combobox
{
get { return _Names; }
set { _Names = value; OnPropertyChanged("Names"); }
}
使用资源中声明的数组:
<Window.Resources>
<x:Array Type="sys:String"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
x:Key="MyArray">
<sys:String>One</sys:String>
<sys:String>Two</sys:String>
</x:Array>
</Window.Resources>
...
<Style x:Key="{x:Type ComboBox}"
TargetType="{x:Type ComboBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=SomeCheckBox, Path=IsChecked}" Value="True">
<Setter Property="ItemsSource" Value="{StaticResource MyArray}" />
</DataTrigger>
</Style.Triggers>
</Style>