简体   繁体   English

如何在UWP XAML中向listboxitem添加功能按钮

[英]How do I add a functioning button to a listboxitem in UWP XAML

I have a UWP C# app with a ListBox. 我有一个带有ListBox的UWP C#应用程序。 I set the ListBox ItemContainerStyle property to a style in a resource. 我将ListBox ItemContainerStyle属性设置为资源中的样式。 I am stuck trying to add a button to the ControlTemplate within the style. 我试图在样式中向ControlTemplate添加一个按钮。 I don't know where to add the Clicked or tapped event handler. 我不知道在哪里添加Clicked或tapped事件处理程序。

I converted the code to use a UserControl added directly to the list and it works great except that the VisualStateManager doesn't work in the UserControl. 我将代码转换为使用直接添加到列表中的UserControl,除了VisualStateManager在UserControl中不起作用外,它工作得很好。

So I can get a functioning button with code-behind for a UserControl and I can get the VisualStateManager to work and handle my custom visualization for the ListBoxItem selections, but I can't get visual states as well as a functioning button. 因此,我可以为UserControl获取一个带有代码隐藏的功能按钮,我可以让VisualStateManager工作并处理ListBoxItem选择的自定义可视化,但是我无法获得视觉状态以及功能按钮。

I'm not sure what code to show here because like I said, everything but the button works fine with a style and everything works fine with a user control except for visual state handling. 我不确定在这里显示什么代码,因为正如我所说的,除了按钮之外的所有内容都可以正常工作,除了可视状态处理外,一切都可以正常使用用户控件。

I have read all the questions I can find about this and the closest I can come is to having a handler function for the button but no one says what class to add it to; 我已经阅读了所有关于此问题的问题,而我最接近的就是为按钮设置处理函数,但是没有人说要添加它的类; they just show the function alone not in any context. 他们只是在任何情境下都不会单独展示这个功能。 And visual state handling always seems to require the developer to detect mouse over, selection, etc..., states to be handled in code with a handler for each state to detect and then a call to GoToState to get the visual state manager to do its thing. 并且可视状态处理似乎总是要求开发人员检测鼠标悬停,选择等...,状态要在代码中处理,每个状态都要检测一个处理程序,然后调用GoToState以让可视状态管理器执行它的东西。 There has got to be a way to do this without all of these gyrations and extra and seemingly redundant code. 必须有一种方法可以做到这一点,没有所有这些旋转和额外的和看似冗余的代码。

[Update] [更新]

I am currently using a style for the ListBoxItem by setting ItemContainerStyle. 我目前通过设置ItemContainerStyle使用ListBoxItem的样式。 Additionally, I am trying to bind the Command parameter of the newly added Button. 另外,我试图绑定新添加的Button的Command参数。 It doesn't work. 它不起作用。 Here's the XAML for the style, showing just the important parts (because it all works as expected except for the button). 这是样式的XAML,只显示重要部分(因为它除按钮外全部按预期工作)。 This shows the two text boxes and the button. 这显示了两个文本框和按钮。 It is important to note that the binding of the Text properties of the TextBox elements works fine and the text shows up exactly as expected. 重要的是要注意TextBox元素的Text属性的绑定工作正常,文本显示完全符合预期。 this makes me want to assume that the binding source and path stuff is all set correctly. 这让我想假设绑定源和路径的东西都设置正确。 But yet again, I an foiled because the Command binding isn't working and no errors are reported at build or run time: 但是又一次,我被挫败了,因为Command绑定不起作用,并且在构建或运行时没有报告错误:

<StackPanel Background="Transparent" Margin="0,0,0,0">
    <TextBlock FontSize="22" TextWrapping="Wrap" FontWeight="SemiLight" x:Name="Title" Text="{Binding Title}" Margin="12,4,24,6" Visibility="{Binding Title, Converter={StaticResource StringToVisibiltyConverter}}" Foreground="{Binding color}"/>
    <TextBlock TextWrapping="Wrap" Visibility="{Binding SubTitle, Converter={StaticResource StringToVisibiltyConverter}}" x:Name="Subtitle" Text="{Binding SubTitle}" Margin="12,-6,24,6" Opacity="0.8" Foreground="{Binding color}"/>
    <Button Command="{Binding ClickCommand}" CommandParameter="x">X</Button>
</StackPanel>

Here is the code for the object that I add to the ListBox: 这是我添加到ListBox的对象的代码:

public class MultilineListboxItem : Object
{
    public MultilineListboxItem() { ClickCommand = new _ClickCommand(); }

    public string Title { get; set; }
    public string SubTitle { get; set; }
    public string Original { get; set; }
    public override string ToString() { return Original; }
    public SolidColorBrush color { get; set; }

    _ClickCommand ClickCommand;
}

And of course, the _ClickCommand class definition: 当然,_ClickCommand类定义:

public class _ClickCommand : ICommand
{
    public void Execute(object parameter)
    {
        Diagnostics.AppendDiagnostic( "derf" );
    }


    public bool CanExecute(object parameter)
    {
        Diagnostics.AppendDiagnostic( "derf" );
        return true;
    }

    public event EventHandler CanExecuteChanged;
}

Those are my own diagnostic reporting functions, not something in C# or WinRT. 这些是我自己的诊断报告功能,而不是C#或WinRT中的功能。

Now that I ma back to using a style to define the look of the list items, the visual states are all working as expected. 现在我回到使用样式来定义列表项的外观,视觉状态都按预期工作。

Try to change ClickCommand to a public property. 尝试将ClickCommand更改为公共属性。

  • The properties you use as binding source properties for a binding must be public properties of your class. 用作绑定的绑定源属性的属性必须是类的公共属性。 Explicitly defined interface properties cannot be accessed for binding purposes, nor can protected, private, internal, or virtual properties that have no base implementation. 无法为绑定目的访问显式定义的接口属性,也不能访问没有基本实现的受保护,私有,内部或虚拟属性。
  • You cannot bind to public fields. 您无法绑定到公共字段。

references: 引用:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM