如何在模板中对输入绑定进行程序化绑定?

问题描述 投票:0回答:2

我需要添加一个 MouseBindingInputBindingsListBoxItems. 我知道如何在xaml中。

<ControlTemplate TargetType="{x:Type ListBoxItem}">
    <Grid>
        <Border x:Name="MyBorder" ...>
            <Border.InputBindings>
                <MouseBinding MouseAction="LeftClick"
                          Command="{Binding SomeCommand}"/>
            </Border.InputBindings>
        </Border>
    </Grid>
    ...
</ControlTemplate>

但我需要知道如何在代码后台写出上述代码。有什么办法可以做到这一点吗?

c# wpf templates code-behind
2个回答
0
投票

我想这就是你要找的。非常好的教程。MSDNhttp:/msdn.microsoft.comen-uslibrarysystem.windows.input.commandbinding.aspx。


0
投票

我知道这不是答案,但如果有人找到了,可以参考一下。https:/stackoverflow.coma909198913179254。

就目前而言,使用 FrameworkElementFactory 微软不推荐使用。使用 XAML 而不是。

一个例子:

<Image.InputBindings>
    <MouseBinding
        MouseAction="LeftClick"
        Command="{Binding ClearCommand, RelativeSource={RelativeSource TemplatedParent}}"
        CommandParameter="{Binding RelativeSource={RelativeSource TemplatedParent}}"/>
</Image.InputBindings>
© www.soinside.com 2019 - 2024. All rights reserved.