我尝试通过相关项的属性设置ListViewItem的可见性,所有这些都发生在我的Listbox-Style中。我的xaml看起来像这样:
<Style TargetType="ListView" x:Key="SideDrawerListViewStyle">
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="ListViewItem">
<i:Interaction.Behaviors>
<c:DataTriggerBehavior Binding="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=DataContext.ItemVisibility}"
Value="Collapsed">
<c:ChangePropertyAction PropertyName="Visibility" Value="Collapsed"/>
</c:DataTriggerBehavior>
</i:Interaction.Behaviors>
</Style>
</Setter.Value>
</Setter>
</Style>
这是使用我制作的样式的ListView
<ListView Style="{StaticResource SideDrawerListViewStyle}"
ItemsSource="{Binding SideDrawerElementList}"
SelectedItem="{Binding SelectedSideDrawerElement, Mode=TwoWay}"
SelectedIndex="{Binding SelectedSideDrawerIndex, Mode=TwoWay}"/>
虽然这是我的ListView-Items ViewModel
public class SideDrawerElementViewModel : ViewModelBase
{
private string _text;
public string Text
{
get => _text;
set => Set(ref _text, value);
}
private string _glyph;
public string Glyph
{
get => _glyph;
set => Set(ref _glyph, value);
}
private Visibility _itemvisibility;
public Visibility ItemVisibility
{
get => _itemvisibility;
set => Set(ref _itemvisibility, value);
}
public string sIV
{ get => ItemVisibility.ToString(); }
public SideDrawerElementViewModel(string content, string glyph, Visibility visibility)
{
Text = content;
Glyph = glyph;
ItemVisibility = visibility;
}
}
我没有收到任何错误,但我的ListView-Item的Visibility-Value从未被调用过。我检查了两次将它设置为Collapsed。还尝试用布尔值或字符串值替换它,但它没有改变任何东西。
因为我正在为UWP写作我正在使用Microsoft.Xaml.Behaviors.Uwp.Managed所以'xmlns:i'是'Microsoft.Xaml.Interactivity'而'xmlns:c'是'Microsoft.Xaml.Interactions 。核心'。我也在使用MvvmLight