在用户控件中定义命令绑定

时间:2015-01-23 13:54:09

标签: c# .net wpf windows datacontext

我用2个按钮和一个复选框编写了用户控件,现在我想将命令绑定到数据上下文 - 对于每个按钮和复选框。 但我不知道如何定义命令绑定。我想我需要在用户控件中使用某种ICommand属性 - 但是如何连接用户的数据上下文命令委托呢?我想使用用户控件来管理集合中的每个项目,如下所示:

<ItemsControl ItemsSource="{Binding Path=MoneyInfo}">
      <ItemsControl.ItemTemplate>
        <DataTemplate>
          <local:ChannelSetupControl 
            CurrentCount="{Binding Count}" 
            CoinValue="{Binding Value}"
            UpCommand="{Binding DataContextUp}" 
            DownCommand="{Binding DataContextDown}" 
            ChangeCheckboxCommand="{Binding DataContextChange}"></local:ChannelSetupControl>
        </DataTemplate>
      </ItemsControl.ItemTemplate>
    </ItemsControl>

XAML用户控件

<UserControl>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="3*"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>

        <TextBlock Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="0" Text="{Binding CoinValue}" TextAlignment="Center"></TextBlock>

        <TextBlock Grid.Column="0" Grid.Row="1" Text="{Binding CurrentCount, Mode=TwoWay}" TextAlignment="Center" VerticalAlignment="Center" FontSize="30"></TextBlock>
        <StackPanel Grid.Column="1" Grid.Row="1" VerticalAlignment="Center">
            <Button Content="+ 10" Padding="0 5"></Button>
            <Button Content="- 10" Padding="0 5"></Button>
        </StackPanel>

        <CheckBox Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="2" IsChecked="{Binding Cycling, Mode=TwoWay}" Content="recycling" VerticalContentAlignment="Center"></CheckBox>
    </Grid>
</UserControl>

和代码背后,这就是我迷失的地方 - 如何定义UpCommand,DownCommand和ChangeCheckboxCommand?

    public partial class ChannelSetupControl : UserControl, INotifyPropertyChanged
    {
        private int currentCount;
        private bool cycling;
        private double coinValue;

        public int Step { get; set; }

        public double CoinValue { get { return coinValue; } set { coinValue = value; NotifyPropertyChanged("CoinValue"); } }
        public int CurrentCount { get { return currentCount; } set { currentCount = value; NotifyPropertyChanged("CurrentCount"); } }
        public bool Cycling { get { return cycling; } set { cycling = value; NotifyPropertyChanged("Cycling"); } }

        public ChannelSetupControl()
        {
            InitializeComponent();
            DataContext = this;

            CurrentCount = 0;
            Step = 10;
            Cycling = false;
            CoinValue = 0;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

1 个答案:

答案 0 :(得分:2)

首先,您的ChannelSetupControl类扩展了UserControl,因此它隐式扩展了DependencyObject类。这意味着您可以使用依赖属性,而不是实现INotifyPropertyChanged

因此,您可以在ChannelSetupControl类中定义依赖项属性,如下所示:

public static readonly DependencyProperty UpCommandProperty =
    DependencyProperty.Register("UpCommand", typeof(ICommand), typeof(ChannelSetupControl));

public ICommand UpCommand
{
    get { return (ICommand)GetValue(UpCommandProperty); }
    set { SetValue(UpCommandProperty, value); }
}

同时在你的控制XAML中:

<Button Command="{Binding RelativeSource={RelativeSource Mode=Self}, Path=UpCommand, Mode=OneWay}"
        Content="+ 10" Padding="0 5" />

以这种方式在您的窗口XAML中,您可以写道:

<local:ChannelSetupControl UpCommand="{Binding UpCommand, Mode=OneWay}" ... />

您可以对其他控件使用相同的“模式”。 关于ICommand,有很多实现。我更喜欢的是所谓的委托命令(对于样本,你可以看看here)。 我希望这个快速解释可以帮到你。