代码之家  ›  专栏  ›  技术社区  ›  Sebastian Busek

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

  •  1
  • Sebastian Busek  · 技术社区  · 10 年前

    我用两个按钮和一个复选框编写了用户控件,现在我想将Commands绑定到数据上下文中——每个按钮和复选框都有。 但我不知道如何定义命令绑定。我想我需要用户控件中的某种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 回复  |  直到 10 年前
        1
  •  2
  •   Il Vic    10 年前

    首先 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 ). 我希望这个简短的解释能对你有所帮助。