Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Custom CommandParameter
Message
De
22/01/2010 21:25:06
 
 
À
22/01/2010 04:58:37
Information générale
Forum:
ASP.NET
Catégorie:
Windows Presentation Foundation (WPF)
Versions des environnements
Environment:
C# 3.0
Divers
Thread ID:
01445153
Message ID:
01445594
Vues:
33
>Hi,
>The aim was to keep the XAML as simple as possible so that the design guys are less likely to get it wrong. But even a stripped down Button looks like this:
<Button>
>            <Button.Resources>
>                <res:NewPaperMultiConverter x:Key="NPMC"/>
>            </Button.Resources>
>            <Button.CommandParameter>
>                <MultiBinding Converter="{StaticResource NPMC}">
>                    <MultiBinding.Bindings>
>                        <Binding ElementName="txtWidth"  Path="Text" />
>                        <Binding ElementName="txtHeight"  Path="Text" />
>                        <Binding ElementName="txtName"  Path="Text" />
>                    </MultiBinding.Bindings>
>                </MultiBinding>
>            </Button.CommandParameter>
>OK</Button>
And, of course, the order of the bindings is critical. I'm normally using a standard MVVM design but in this case the three parameters don't have a natural mapping to the VM - they are just used to create an object (which is in the VM). I think I'm just going to stick with the pattern and extend the VM to enable the designers to bind the textboxes directly to it - at least they are familiar with that.
>
>I had a blurry idea of sub-classing a button with a dependency property that would hold a collection of parameters which could then be used in a generic fashion but I'm not sure the above type of scenario would crop up often enough to warrant the effort.

Wouldn't this work for you?

XAML:
<Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="Window1" SizeToContent="WidthAndHeight">
  <Window.Resources>
    <local:PaperInfo Name="New Paper"
                     Height="11"
                     Width="8.5"
                     x:Key="PaperData"/>
  </Window.Resources>
  <Grid>
    <StackPanel>
      <TextBox Text="{Binding Source={StaticResource PaperData}, Path=Name}"/>
      <TextBox Text="{Binding Source={StaticResource PaperData}, Path=Height}"/>
      <TextBox Text="{Binding Source={StaticResource PaperData}, Path=Width}"/>
      <Button Command="{x:Static local:Window1.DoIt}"
              CommandParameter="{Binding Source={StaticResource PaperData}, Path=.}">Test</Button>
    </StackPanel>
  </Grid>
</Window>
C#:
using System.Windows;
using System.Windows.Input;

namespace WpfApplication1
  {
  /// <summary>
  /// Interaction logic for Window1.xaml
  /// </summary>
  public partial class Window1 : Window
    {
    public static RoutedCommand DoIt = new RoutedCommand();

    public Window1()
      {
      this.CommandBindings.Add(new CommandBinding(DoIt,
                                                  DoItExecuted,
                                                  DoItCanExecute));
      InitializeComponent();
      }

    public void DoItExecuted(object target, ExecutedRoutedEventArgs e)
      {
      if (e.Parameter is PaperInfo)
        {
        MessageBox.Show(((PaperInfo)e.Parameter).Name);
        }
      }

    public void DoItCanExecute(object sender, CanExecuteRoutedEventArgs e)
      {
      e.CanExecute = true;
      }
    }

  public class PaperInfo 
    {
    public string Name   { get; set; }
    public double Width  { get; set; }
    public double Height { get; set; }
    }
  }
Simplest idea I can come up with.

HTH
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform