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

Define command binding in user control

我用 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));
            }
        }
    }

首先你的 ChannelSetupControl class 扩展了 UserControl,所以它隐含地扩展了 DependencyObject class。这意味着您可以使用 Dependency Properties 而不是实施 INotifyPropertyChanged.

所以你可以在你的 ChannelSetupControl class 中定义一个依赖项 属性,就像这样:

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" />

这样在你的windowXAML中你可以这样写:

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

您可以对其他控件使用相同的 "pattern"。 关于ICommand,有很多种实现方式。我更喜欢的是所谓的委托命令(有关示例,您可以看一下 here)。 我希望这个快速解释可以帮助你。