首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >AvaloniaUI绑定到ItemsSource之外的命令

AvaloniaUI绑定到ItemsSource之外的命令
EN

Stack Overflow用户
提问于 2021-12-14 11:02:47
回答 1查看 261关注 0票数 1

我有个问题。我想给一个按钮一个来自ViewModel的命令,它位于ItemsRepeater ItemsSource之外。我需要关于如何进行这种绑定的帮助。

在我的ItemsRepeater按钮

<Button Command={Binding TestClick} Grid.Column="0" HorizontalAlignment="Stretch" Foreground="#6264a7" HorizontalContentAlignment="Center" CornerRadius="0" Background="#2f2f2f" FontSize="20">Details</Button>

我的ItemsRepeater

代码语言:javascript
运行
复制
                            <ItemsRepeater.ItemTemplate>
                                <DataTemplate>
                                    <DockPanel Margin="30,0,30,50">
                                        <StackPanel>
                                            <TextBlock Background="#2f2f2f" FontSize="25" Foreground="AntiqueWhite" TextAlignment="Center" Padding="0,8,0,8" Text="{Binding Name}"></TextBlock>
                                            <TextBlock TextAlignment="Center" Background="#2f2f2f" Foreground="AntiqueWhite" Height="40" FontSize="20" Padding="0,8,0,0" Text="{Binding Date}"></TextBlock>
                                            <TextBlock TextAlignment="Center" Background="#2f2f2f" Foreground="AntiqueWhite" Height="40" FontSize="20" Padding="0,2,0,0" Text="{Binding EventType}"></TextBlock>
                                            <Grid>
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="50*" />
                                                    <ColumnDefinition Width="50*" />
                                                </Grid.ColumnDefinitions>

                                                <Button Command={Binding TestClick} Grid.Column="0" HorizontalAlignment="Stretch" Foreground="#6264a7" HorizontalContentAlignment="Center" CornerRadius="0" Background="#2f2f2f" FontSize="20">Details</Button>
                                                <Button Grid.Column="1" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" Foreground="#a4373a" CornerRadius="0" Background="#2f2f2f" FontSize="20">Delete</Button>

                                            </Grid>

                                            <ProgressBar Height="10" CornerRadius="0" Value="{Binding TimeLeft}" Minimum="0" Maximum="{Binding DifferenceBetweenDates}" Foreground="{Binding ProgressBarColour}" />
                                        </StackPanel>
                                    </DockPanel>
                                </DataTemplate>
                            </ItemsRepeater.ItemTemplate>
                        </ItemsRepeater>

我的ViewModel:

代码语言:javascript
运行
复制
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Anniverse.ViewModels
{
    class MainPanelViewModel : ViewModelBase
    {
        public string CurrentDate => DateTime.Today.ToString("dd.MM.yyyy");

        public ObservableCollection<Event> Events => new Connector().GetEvents();

        public void TestClick() 
        {
            Console.WriteLine("Hello test");
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-15 10:55:26

实现这一目标的最简单方法是绑定到祖先。

代码语言:javascript
运行
复制
Command="{Binding $parent[ItemsRepeater].DataContext.YourCommand}"

注意,您需要在视图模型中为命令绑定定义ICommand以使其工作,您可以找到一个示例这里

编辑:我没有意识到这一点,但是直接绑定到方法也应该有效。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70347801

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档