Trigger to set selected ListView through XAML in UWP

I have a ListView containing TextBoxes and I would like to set the selected item whenever the user clicks one of the textboxes.

Here is what I have tried so far.

<StackPanel Orientation="Vertical">

            <TextBlock Text="Name  Serial"/>
            <ListView ItemsSource="{Binding Items, Mode=OneWay}"
                      SelectedItem="{x:Bind VM.SelectedItem, Mode=TwoWay}">
                <ListView.Resources>
                    <Style TargetType="ListViewItem">
                        <Style.Triggers>
                            <Trigger Property="IsKeyboardFocusWithin" Value="True">
                                <Setter Property="IsSelected" Value="True"/>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </ListView.Resources>
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBox Text="{Binding Name, Mode=TwoWay}"/>
                            <TextBox Text="{Binding Serial, Mode=TwoWay}"/>
                        </StackPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackPanel>

      

The problem is that style triggers are not supported in UWP. Is there any other way that I can achieve this?

+3


source to share


1 answer


It can usually Trigger

be replaced with Behavior

s. Here is an example -

You need to install this nuget first if you don't have it.

Install-Package Microsoft.Xaml.Behaviors.Uwp.Managed

      

Then you want to add these namespaces to your XAML.

xmlns:Interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:Core="using:Microsoft.Xaml.Interactions.Core"

      



In these namespaces you can now use the EventTriggerBehavior

one that comes with the nuget package you installed earlier. You basically need to attach it to all your TextBox

es that manage the selection.

<TextBox>
    <Interactivity:Interaction.Behaviors>
        <Core:EventTriggerBehavior EventName="GotFocus">
            <local:SelectSelectorItemAction />
        </Core:EventTriggerBehavior>
    </Interactivity:Interaction.Behaviors>
</TextBox>

      

What's EventTriggerBehavior

in this case, whenever an event is called GotFocus

for , the called (through ) TextBox

will be executed . This is the only thing you will need to build.IAction

SelectSelectorItemAction

IAction.Execute

SelectSelectorItemAction

public class SelectSelectorItemAction : DependencyObject, IAction
{
    public object Execute(object sender, object parameter)
    {
        var textBox = (FrameworkElement)sender;
        var selectorItem = textBox.GetParent<SelectorItem>();

        selectorItem.IsSelected = true;
        return true;
    }
}

public static class Extensions
{
    public static T GetParent<T>(this DependencyObject element) where T : DependencyObject
    {
        var parent = VisualTreeHelper.GetParent(element);

        // C# 7 pattern matching feature. If you are not using C# 7, change it.
        if (parent is T p)
        {
            return p;
        }

        return GetParent<T>(parent);
    }
}

      

The code is pretty simple. All it does is search the Visual Tree and search SelectorItem

, which is the base class ListViewItem

. This way you can reuse the same one SelectSelectorItemAction

for GridView

.

+3


source







All Articles