没有按钮的 MVVM 命令

发布于 2024-09-27 12:04:33 字数 342 浏览 2 评论 0原文

我想启动绑定命令,但不想在 UserControl 上使用 Button。实际上,UserControl 本身就是一个Button。当单击其表面上的任意位置时,我想启动绑定命令。有没有办法给UserControl一个命令?

旁注:一个命令用于一个控件,并且只有几个特定的​​开箱即用控件?这一切看起来有点笨拙。我开始认为 MVVM 不切实际。我可以通过接口和 OOP 很好地解耦我的 UI。无论如何,我还是有希望的。

另外,我不愿意破解任何东西或使用昂贵的解决方法。如果我做不到这一点,我就会放弃 MVVM。

I want to initiate a bound command, but I don't want to use a Button on my UserControl. In effect the UserControl is itself a Button. When it is clicked anywhere on its surface I want to initiate a bound command. Is there a way to give a UserControl a command?

In a side note: one command for one control and only a few certain out-of-the-box controls? This all seems a little clunky. I'm starting to think that MVVM is impractical. I can decouple my UI just fine with Interfaces and OOP. Anyway, I still have hope.

Also, I'm not willing to hack anything or use an expensive workaround. If I can't do this, I'm abandoning MVVM.

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

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

发布评论

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

评论(5

烛影斜 2024-10-04 12:04:33

在此处查看 ICommandSource 界面:http:// msdn.microsoft.com/en-us/library/system.windows.input.icommandsource.aspx。如果你想让一个控件有一个命令,那么你的控件应该实现这个接口。实现此接口的控件的示例是 ButtonBase 和 MenuItem。希望这有帮助。

Take a look at the ICommandSource interface here: http://msdn.microsoft.com/en-us/library/system.windows.input.icommandsource.aspx. If you want a control to have a command, then your control should implement this interface. Examples of controls that implement this interface are ButtonBase and MenuItem. Hope this helps.

完美的未来在梦里 2024-10-04 12:04:33

如果您的 UserControl 本质上是一个 Button,那么您为什么要编写自己的 UserControl 而不是使用 Button 类?

要添加更多信息,您可以执行以下操作:

  1. 子类 Button,将您需要的任何额外 DependencyProperties 放入其中 - 它应该是一个非常空的类(您甚至可以有类似 public class MyCoolButton : Button { }
  2. 添加 TargetType 为 MyCoolButton 的样式 - 不要命名该样式,以便它适用于所有 MyCoolButton
  3. 覆盖该样式的默认模板,然后粘贴到您的 Xaml 代码中 您可能需要做一些工作。此处处理“正常/推送/禁用”状态。如果您使用的是 v4.0,则可以在此处使用 VSM。

If your UserControl is essentially a Button, why are you writing your own UserControl instead of using the Button class?

To add more info, here's what you do:

  1. Subclass Button, put any extra DependencyProperties that you need in there - it should be a very empty class (you could even have something like public class MyCoolButton : Button { }
  2. Add a Style whose TargetType is MyCoolButton - don't name the style so it applies to all MyCoolButtons
  3. Override the default Template of the style, then paste in your Xaml code. You might have to do some work here to handle the "Normal / Pushed / Disabled" states. If you're using v4.0, you can use VSM here.
°如果伤别离去 2024-10-04 12:04:33

我同意保罗·贝茨的观点。

我经常使用按钮作为顶部容器来创建自己的 ListBoxItemContainerStyle,其中除了无属性的内容呈现器之外什么都没有。这使我可以使用按钮功能(如命令),而无需安装 Windows chrome。

将其放入 ListBoxItemContainerStyle 中还可以让我在单击它时不会显示正常的虚线边框 (FocusVisualStyle={x:Null})。

您是否使用 Visual Studio 或 Expression Blend 进行样式设计?

此外,一些 MVVM 框架提供了一个接口,用于添加命令式的功能来控制除按钮之外的控件。 Caliburn 有相当丰富的命令模式。但是,我不确定它是否允许在非按钮控件上绑定命令。

I will agree with Paul Betts.

Quite often I create my own ListBoxItemContainerStyle using a button as the top container with nothing but a propertyless content presenter in it. This allows me to use the buttons functionality (like Command) without having the Windows chrome on it.

Putting it in the ListBoxItemContainerStyle also lets me make it so that when it is clicked it does not display the normal dotted border (FocusVisualStyle={x:Null}).

Are you using Visual Studio or Expression Blend to do your styling?

Additionally, some MVVM frameworks provide an interface for adding a command-ish ability to controls other than buttons. Caliburn has a pretty rich command pattern. I am not sure if it allows binding commands on non-button controls, however.

蛮可爱 2024-10-04 12:04:33

OP 要求提供一个示例,说明如何使用按钮控件,但内容正确填充整个按钮。您可以使用 ContentAlignment 属性来执行此操作:

<Button HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
    <Button.Content>
        <Grid IsHitTestVisible="False">
            <Grid.RowDefinitions>
                <RowDefinition Height="30" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <TextBlock Grid.Row="0" Text="Row0" />
            <TextBlock Grid.Row="1" Text="Row1" />
        </Grid>
    </Button.Content>
</Button>

这将创建一个带有两个使用网格控件间隔的标签的按钮。我将网格标记为关闭 HitTestVisible,因为您必须决定哪些控件应该像按钮一样交互,哪些控件应该像控件一样交互。例如,您可能有一个嵌入式 TextBox,您希望无需单击按钮即可单击该 TextBox,在这种情况下,它应该具有 HitTestVisible=True。

The OP asked for an example of how you could use a button control, but with the content properly filling the entire button. You can do this using the ContentAlignment properties:

<Button HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
    <Button.Content>
        <Grid IsHitTestVisible="False">
            <Grid.RowDefinitions>
                <RowDefinition Height="30" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <TextBlock Grid.Row="0" Text="Row0" />
            <TextBlock Grid.Row="1" Text="Row1" />
        </Grid>
    </Button.Content>
</Button>

This creates a button with two labels spaced using a Grid control. I mark the Grid to turn off HitTestVisible, as you have to decide which controls should interact like the button and which should interact like controls. For instance, you might have an embedded TextBox that you want to be clickable without clicking the button, in which case it should have HitTestVisible=True.

抚笙 2024-10-04 12:04:33

WPF 支持图层和透明度:

Panel.ZIndex

您可以创建任何支持在高级透明图层上进行命令的任何内容(您想要的大小),以充当按钮。

WPF supports layers and transparency :

Panel.ZIndex

You can create anything that supports commanding on a superior transparent layer, the size you want, to act as a button.

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