本文介绍了在DataTemplate UWP中绑定ComboBoxItem的IsSelected属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试绑定ComboBoxItem的IsSelected属性,以便显示正确的图标,如果选择了Item,应该会显示红色图标,否则打开的列表的睡觉会显示灰色。下面的代码显示了我如何尝试实现该功能。<ComboBox
ItemsSource="{Binding Icons}"
SelectedItem="{Binding SelectedIcon, Mode=TwoWay}"
Style="{ThemeResource DefaultComboBoxStyle}"
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image
Width="20"
Height="20"
Source="{Binding IconId, Mode=TwoWay, Converter={StaticResource IconConverter}, ConverterParameter={StaticResource True}}"
Visibility="{Binding Path=IsSelected,RelativeSource={RelativeSource Mode=TemplatedParent}, Converter={StaticResource BoolToVisibilityConverter}}" />
<Image
Width="20"
Height="20"
Source="{Binding IconId, Mode=TwoWay, Converter={StaticResource IconConverter}}"
Visibility="{{Binding Path=IsSelected,RelativeSource={RelativeSource Mode=TemplatedParent} Converter={StaticResource BoolToVisibilityInverted}}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
一个项目的图标id对于两个图像是相同的,转换器将根据传递的参数返回正确的图像链接。我已尝试获取父数据上下文并尝试使用comboBoxItem(SelectorItem)的IsSelected属性,但不起作用。我不想在要迭代的ViewModel中添加IsSelected属性,我假设存在某些语法,但我找不到它。我只想获取属性IsSelected的值,并在不更改ViewModel中的代码的情况下绑定到XAML中。
实现了类似以下代码中的内容,但实现了硬编码元素。根据ComboBoxItem的IsSelected Item属性,会显示正确的图标。我希望使用ItemsSource和DataTemplate实现相同的功能。 <ComboBox
SelectedIndex="{Binding IconType, Mode=TwoWay, Converter={StaticResource IconTypeIndexToIconConverter}}"
Style="{ThemeResource DefaultComboBoxStyle}">
<ComboBoxItem x:Name="SwitchIcon">
<StackPanel Orientation="Horizontal">
<Image
Source="{Binding Source=1, Converter={StaticResource IconConverter}}"
Style="{ThemeResource DeviceTypeImageBlockStyle}"
Visibility="{Binding IsSelected, ElementName=SwitchIcon, Converter={StaticResource BoolToVisibilityInverted}}" />
<Image
Source="{Binding Source=1, Converter={StaticResource IconConverter}, ConverterParameter={StaticResource True}}"
Style="{ThemeResource DeviceTypeImageBlockStyle}"
Visibility="{Binding IsSelected, ElementName=SwitchIcon, Converter={StaticResource BoolToVisibilityConverter}}" />
</StackPanel>
</ComboBoxItem>
<ComboBoxItem x:Name="LightDimmingIcon">
<StackPanel Orientation="Horizontal">
<Image
Source="{Binding Source=2, Converter={StaticResource IconConverter}}"
Style="{ThemeResource DeviceTypeImageBlockStyle}"
Visibility="{Binding IsSelected, ElementName=LightDimmingIcon, Converter={StaticResource BoolToVisibilityInverted}}" />
<Image
Source="{Binding Source=2, Converter={StaticResource IconConverter}, ConverterParameter={StaticResource True}}"
Style="{ThemeResource DeviceTypeImageBlockStyle}"
Visibility="{Binding IsSelected, ElementName=LightDimmingIcon, Converter={StaticResource BoolToVisibilityConverter}}" />
</StackPanel>
</ComboBoxItem>
</ComboBox>
有什么想法吗?解决方案?
推荐答案
恐怕您无法绑定DataTemplate
UWP中ComboBoxItem
的IsSelected
属性。因为TemplatedParent RelativeSource是ContentPresenter
而不是ComboBoxItem
,您可以使用下面的XAML进行验证。<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Border DataContext="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}}">
<TextBlock x:Name="Print" Text="{Binding}"/>
</Border>
<TextBlock Text="{Binding}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
它将在Print
文本块中显示Windows.UI.Controls.ContentPresenter
。
IsSlected
添加到模型类来处理该场景。并获得选择项,修改IsSelected值以影响UI。下面我做了完整的样品。代码隐藏
public sealed partial class MainPage : Page, INotifyPropertyChanged
{
public List<CBContent> Items { get; set; } = new List<CBContent>();
public MainPage()
{
this.InitializeComponent();
Items.Add(new CBContent { Content = "Item1", IsSelected = false });
Items.Add(new CBContent { Content = "Item2", IsSelected = false });
Items.Add(new CBContent { Content = "Item3", IsSelected = false });
Items.Add(new CBContent { Content = "Item4", IsSelected = false });
Items.Add(new CBContent { Content = "Item5", IsSelected = false });
Items.Add(new CBContent { Content = "Item6", IsSelected = false });
this.DataContext = this;
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private CBContent _selectItem;
private CBContent _previousSelectItem;
public CBContent SelectItem
{
get
{
return _selectItem;
}
set
{
_selectItem = value;
value.IsSelected = true;
if(_previousSelectItem != null)
{
_previousSelectItem.IsSelected = false;
}
_previousSelectItem = _selectItem;
OnPropertyChanged();
}
}
}
public class CBContent : INotifyPropertyChanged
{
public string Content { get; set; }
private bool _isSelect;
public bool IsSelected
{
get
{
return _isSelect;
}
set
{
_isSelect = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
XAML
<Grid>
<ComboBox ItemsSource="{Binding Items, Mode=OneWay}" SelectedItem="{Binding SelectItem,Mode=TwoWay}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Rectangle
Width="20"
Height="20"
Fill="Red"
Visibility="{Binding IsSelected, Mode=TwoWay}"
/>
<TextBlock Text="{Binding Content}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
这篇关于在DataTemplate UWP中绑定ComboBoxItem的IsSelected属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!