开发者

MVVM Command without A Button

开发者 https://www.devze.com 2023-01-19 17:56 出处:网络
I want to initiate a bound command, but I don\'t want to use a Button on my UserCo开发者_JAVA技巧ntrol. In effect the UserControl is itself a Button.When it is clicked anywhere on its surface I want t

I want to initiate a bound command, but I don't want to use a Button on my UserCo开发者_JAVA技巧ntrol. 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.


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.


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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号