UWP - ListBoxItem按钮可见性问题

时间:2016-03-23 16:24:42

标签: listbox win-universal-app itemtemplate

我正在写一个uwp应用程序。主页面包含一个列表框。每个列表项都显示一行文本和一个按钮。该按钮的可见性为“已折叠”,我希望在选择该项目时将其可见性更改为“可见”。

由于uwp中没有样式触发器,到目前为止,我已尝试在#!/bin/bash clear #return all function all { cat people.dat } #return some function search { grep -i "$SEARCH" people.dat || echo "search returned nothing" } #or search function or { egrep -i "$search1|$search2" people.dat } #and search function and { if [[ $(grep -i "$search1") = $(grep -i "$search2") ]]; then echo yes #that is temporary I want to see if it worked fi } #return null function null { return } while [ true ] do #get the search read SEARCH #search if [[ $SEARCH == *" OR "* ]]; then search1=${SEARCH%" OR "*} search2=${SEARCH##*" OR "} or elif [[ $SEARCH == *" AND "* ]]; then search1=${SEARCH%" AND "*} search2=${SEARCH##*" AND "} and elif [ "$SEARCH" = "all" }; then all elif [ "$SEARCH" = "exit" ]; then exit elif [ "$SEARCH" = "" ]; then null else search fi done DataTemplate定义的ListBox内使用VisualStateManager。那没用。

我尝试将ItemTemplateVisualStateManager的{​​{1}}定义一起使用。这也不起作用。

在我看来,这很简单,但我找不到任何办法。有没有人有任何想法?

3 个答案:

答案 0 :(得分:0)

如果使用MVVM和Binding方法,可以使用DataTriggerBehaviors和ChangePropertyAction来更改按钮的可见性

您需要安装Nuget包 Microsoft.xaml.behaviors.uwp.managed

https://www.nuget.org/packages/Microsoft.Xaml.Behaviors.Uwp.Managed/

或者最简单的方法是使用Blend打开您的页面或UserControl并查找行为,并为您的必要包安装混合使用它。

https://blogs.msdn.microsoft.com/wsdevsol/2014/12/10/using-a-datatriggerbehavior-to-change-an-itemtemplate-in-a-windows-store-app/

更新

你不能在ItemDataTemplate里面使用VisualStateManager但不支持但是要做一些使用它的技巧

AdaptiveTrigger and DataTemplate

答案 1 :(得分:0)

如果您使用的是 MVVM 模式,则可以为您绑定到IsSelected的商品添加额外的ListBox媒体资源:

public bool IsSelected
{
    get { return _isSelected; }
    set
    {
        _isSelected = value;
        RaisePropertyChanged();
    }
}

在页面视图模型上添加SelectedItem属性,该属性会将IsSelected正确设置为所有项目:

public Item SelectedItem
{
    get { return _selectedItem; }
    set
    {
        if (_selectedItem != value)
        {
            if (_selectedItem != null)
            {
                _selectedItem.IsSelected = false;
            }
            if (value != null)
            {
                value.IsSelected = true;
            }
            _selectedItem = value;
            RaisePropertyChanged();
        }
    }
}

现在您需要绑定这两个属性:

<ListBox ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Name}" />
                <Button Content="Click Me" Visibility="{Binding IsSelected, Converter={StaticResource VisibilityConverter}}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

您还需要一个转换器,用于将bool值转换为Visibility

public class VisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        if (value is bool)
        {
            return (bool) value ? Visibility.Visible : Visibility.Collapsed;
        }
        else
        {
            return Visibility.Collapsed;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new System.NotImplementedException();
    }
}

将其声明为页面上的静态资源:

<Page.Resources>
    <local:VisibilityConverter x:Key="VisibilityConverter" />
</Page.Resources>

没有MVVM模式,您最好使用Cimbalino multibinding behavior。首先参考Cimbalino Toolkit。然后,您可以在XAML中配置多重绑定:

<ListBox ItemsSource="{Binding Items}" x:Name="MyListBox">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Name}" />
                <Button Content="Click Me">
                    <interactivity:Interaction.Behaviors>
                        <behaviors:MultiBindingBehavior PropertyName="Visibility" Converter="{StaticResource VisibilityMultiConverter}">
                            <behaviors:MultiBindingItem Value="{Binding}"/>
                            <behaviors:MultiBindingItem Value="{Binding SelectedItem, ElementName=MyListBox}"/>
                        </behaviors:MultiBindingBehavior>
                    </interactivity:Interaction.Behaviors>
                </Button>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>

您需要一个转换器来检查当前是否选择了该项:

public class VisibilityMultiConverter : MultiValueConverterBase
{
    public override object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        return values[0] != null && values[0] == values[1] ? Visibility.Visible : Visibility.Collapsed;
    }

    public override object[] ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new System.NotImplementedException();
    }
}

将转换器添加为页面资源:

<Page.Resources>
    <local:VisibilityMultiConverter x:Key="VisibilityMultiConverter" />
</Page.Resources>

答案 2 :(得分:0)

这似乎是一篇旧文章,但仍在为新手解答:)

您只需在.xaml页面的ListBox中使用SelectionChanged属性就很容易

例如:

ButtonVisibility.xaml

<ListBox
    x:Name="ListBox"
    HorizontalAlignment="Stretch"
    VerticalAlignment="Stretch"
    SelectionMode="Single"
    SelectionChanged="ListBox_SelectionChanged">
</ListBox>

现在在您的代码后面调用SelectionChanged事件

ButtonVisibility.xaml.cs

private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    YourButton.Visibility = Visibility.Visible;
}