wpf - 在 WPF 中,我想根据组合框选择动态设置 itemsource
问题描述
我的视图中有一个Slider和一个Combobox。我的 ViewModel 中有 2 个属性。基于组合框的选择,我想将任何一个属性绑定到value
滑块的。
private int _xValue;
public int XValue
{
get { return _xValue; }
set
{
_xValue = value;
NotifyPropertyChanged();
}
}
private int _yValue;
public int YValue
{
get { return _yValue; }
set
{
_yValue = value;
NotifyPropertyChanged();
}
}
<StackPanel>
<ComboBox SelectedIndex="0" Margin="2" Width="100">
<ComboBoxItem Tag="X">X</ComboBoxItem>
<ComboBoxItem Tag="Y">Y</ComboBoxItem>
</ComboBox>
<Slider Value="{Binding XValue}"></Slider>
</StackPanel>
我想将Slider value
绑定到XValue
或YValue
取决于 ComboBox 的选择
解决方案
您可以使用 aStyle
与 aDataTrigger
绑定到SelectedItem
的ComboBox
:
<ComboBox x:Name="cmb" SelectedIndex="0" Margin="2" Width="100">
<ComboBoxItem Tag="X">X</ComboBoxItem>
<ComboBoxItem Tag="Y">Y</ComboBoxItem>
</ComboBox>
<Slider>
<Slider.Style>
<Style TargetType="Slider">
<Setter Property="Value" Value="{Binding XValue}" />
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedItem.Tag, ElementName=cmb}" Value="Y">
<Setter Property="Value" Value="{Binding YValue}" />
</DataTrigger>
</Style.Triggers>
</Style>
</Slider.Style>
</Slider>
推荐阅读
- c# - EF Core One to Many adding new object
- c# - 在 Any at = IsAvailable = !b.BorrowHistories.Any(h => h.ReturnDate == null) 下获得一条红线
- c - INI 文件阅读器功能正在向我的结构寻址错误的值,我该如何解决?
- mysql - 如何在查询中执行递归查询或子查询?
- reactjs - 如何设置 react-final-form onSubmit 值参数类型 TypeScript
- javascript - 如何处理按钮按下时的输入数据?
- reactjs - 如何在 Tabnav 上方添加标题
- python - 检查目标时出错:预期 activation_1 的形状为 (1,),但数组的形状为 (10,)
- c# - 如何返回所有列的名称?
- git - 在单个比较窗口中比较多个文件的差异