将按钮命令绑定到父级数据上下文以外的源

发布于 2024-10-02 07:41:54 字数 967 浏览 0 评论 0原文

我正在尝试将数据上下文内的按钮命令绑定到主视图模型中的 RelayCommand。当我使用 Blend 时,它会更改包含网格的数据上下文以指向主视图模型,从而破坏网格内容的绑定,网格内容应基于行的视图模型等。我尝试了以下操作,但编译器说我是缺少属性名称。

<Button Style="{StaticResource StatusButtonStyle}">
                    <Button.Command>
                        <Binding Source="{StaticResource Locator" Path="{Main.ViewStatusCommand}" Mode="OneWay"                 
                    </Button.Command>
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="Click">
                            <i:InvokeCommandAction/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </Button>

我也尝试过这个:

<Button Style="{StaticResource StatusButtonStyle}" Command="{Binding Main.ViewStatusCommand, Source=StaticResource Locator}">

但这也不起作用。如果我不允许设置命令绑定的来源,有人可以解释一下原因吗?谢谢。

I am trying to bind the command of a button inside a datacontext to a RelayCommand in the Main viewmodel. When I use Blend it changes the datacontext of the containing grid to point to the Main ViewModel thus breaking the binding of the content of the grid which should be based on the viewmodel of the row etc. I tried the following but the compiler says I am missing an attribute name.

<Button Style="{StaticResource StatusButtonStyle}">
                    <Button.Command>
                        <Binding Source="{StaticResource Locator" Path="{Main.ViewStatusCommand}" Mode="OneWay"                 
                    </Button.Command>
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="Click">
                            <i:InvokeCommandAction/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </Button>

I also tried this:

<Button Style="{StaticResource StatusButtonStyle}" Command="{Binding Main.ViewStatusCommand, Source=StaticResource Locator}">

But that doesn't work either. If I am not alowed to set the source of the command binding, could someone please explain why? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

≈。彩虹 2024-10-09 07:41:54

您忘记了第二个示例中的一些括号(第一个示例在 Binding 元素中缺少 /> )

<Button Style={StaticResource StatusButtonStyle}" Command="{Binding Main.ViewStatusCommand, Source={StaticResource Locator}}">

这是否是解释它不起作用的原因?如果不是这种情况,请描述一下您的症状(当我按下按钮时没有任何反应,有一个异常(如果是这种情况,什么时候,什么异常?))

如果按下按钮时没有任何反应,看看在调试控制台上检索绑定错误。

You forgot some brackets in the second exemple (and the first lack a /> in the Binding element )

<Button Style={StaticResource StatusButtonStyle}" Command="{Binding Main.ViewStatusCommand, Source={StaticResource Locator}}">

Is it the reason explaining why it don't work? If it's not the case, could you please describe your symptoms (nothing happen when I push the button, there is an Exception (if this is the case, when, and what exception? ) )

If nothing happen when you push the button, look at the debugging console to retreive the Bindings errors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文