UWP IsNullOrEmptyStateTrigger 不适用于 ListView
UWP IsNullOrEmptyStateTrigger not working for ListView
下面的 XAML 适用于 UWP 应用程序,它使用带有 Microsoft.Toolkit.Uwp.UI 包中的 IsNullOrEmptyStateTrigger
的 VisualStateManager。当没有从 ListView 中选择任何项目时,触发器应该禁用按钮。但是,该按钮始终保持启用状态。
当列表中没有任何内容被选中时,ListView 的 SelectedItem
应该为空(当您第一次 运行 应用程序时没有被选中)。有什么想法吗?
我的代码大致基于 this example。
<Page
x:Class="UwpTriggerDemo.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UwpTriggerDemo"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:triggers="using:Microsoft.Toolkit.Uwp.UI.Triggers"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="ListViewStates">
<VisualState x:Name="ListSomethingSelectedState" />
<VisualState x:Name="ListNothingSelectedState">
<VisualState.StateTriggers>
<triggers:IsNullOrEmptyStateTrigger Value="{Binding SelectedItem,
ElementName=myListView, Mode=OneWay}"/>
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target="myButton.IsEnabled" Value="False" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ListView x:Name="myListView">
<TextBlock Text="Item 1" />
<TextBlock Text="Item 2" />
<TextBlock Text="Item 3" />
</ListView>
<Button x:Name="myButton" Content="Click Me" />
</StackPanel>
</Page>
UWP IsNullOrEmptyStateTrigger not working for ListView
我们可以重现您的问题,请在 WindowsCommunityToolkit
问题框中随意 post 这个问题。目前,这是一种手动控制 myButton 状态的解决方法。在后面的代码中制作 IsEnable
属性 并将其绑定到 myButton。并更新 SelectItem
set 方法中的 IsEnable 值。更多请参考以下代码
private bool _isEnable;
public bool IsEnable
{
get
{
return _isEnable;
}
set
{
_isEnable = value;
OnPropertyChanged();
}
}
private string _selectItem;
public string SelectItem
{
get
{
return _selectItem;
}
set
{
_selectItem = value;
if(_selectItem == null)
{
IsEnable = false;
}
else
{
IsEnable = true;
}
OnPropertyChanged();
}
}
Xaml
<ListView
x:Name="myListView"
IsItemClickEnabled="True"
SelectedItem="{Binding SelectItem, Mode=TwoWay}">
<x:String>Hello</x:String>
<x:String>Hello</x:String>
<x:String>Hello</x:String>
<x:String>Hello</x:String>
</ListView>
<Button
x:Name="myButton"
Content="Click Me"
IsEnabled="{Binding IsEnable}" />
下面的 XAML 适用于 UWP 应用程序,它使用带有 Microsoft.Toolkit.Uwp.UI 包中的 IsNullOrEmptyStateTrigger
的 VisualStateManager。当没有从 ListView 中选择任何项目时,触发器应该禁用按钮。但是,该按钮始终保持启用状态。
当列表中没有任何内容被选中时,ListView 的 SelectedItem
应该为空(当您第一次 运行 应用程序时没有被选中)。有什么想法吗?
我的代码大致基于 this example。
<Page
x:Class="UwpTriggerDemo.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UwpTriggerDemo"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:triggers="using:Microsoft.Toolkit.Uwp.UI.Triggers"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="ListViewStates">
<VisualState x:Name="ListSomethingSelectedState" />
<VisualState x:Name="ListNothingSelectedState">
<VisualState.StateTriggers>
<triggers:IsNullOrEmptyStateTrigger Value="{Binding SelectedItem,
ElementName=myListView, Mode=OneWay}"/>
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target="myButton.IsEnabled" Value="False" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ListView x:Name="myListView">
<TextBlock Text="Item 1" />
<TextBlock Text="Item 2" />
<TextBlock Text="Item 3" />
</ListView>
<Button x:Name="myButton" Content="Click Me" />
</StackPanel>
</Page>
UWP IsNullOrEmptyStateTrigger not working for ListView
我们可以重现您的问题,请在 WindowsCommunityToolkit
问题框中随意 post 这个问题。目前,这是一种手动控制 myButton 状态的解决方法。在后面的代码中制作 IsEnable
属性 并将其绑定到 myButton。并更新 SelectItem
set 方法中的 IsEnable 值。更多请参考以下代码
private bool _isEnable;
public bool IsEnable
{
get
{
return _isEnable;
}
set
{
_isEnable = value;
OnPropertyChanged();
}
}
private string _selectItem;
public string SelectItem
{
get
{
return _selectItem;
}
set
{
_selectItem = value;
if(_selectItem == null)
{
IsEnable = false;
}
else
{
IsEnable = true;
}
OnPropertyChanged();
}
}
Xaml
<ListView
x:Name="myListView"
IsItemClickEnabled="True"
SelectedItem="{Binding SelectItem, Mode=TwoWay}">
<x:String>Hello</x:String>
<x:String>Hello</x:String>
<x:String>Hello</x:String>
<x:String>Hello</x:String>
</ListView>
<Button
x:Name="myButton"
Content="Click Me"
IsEnabled="{Binding IsEnable}" />