c# - 在用户控件中定义命令绑定(bind)

标签 c# .net wpf windows datacontext

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

<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 类扩展了 UserControl,因此它隐式地扩展了 DependencyObject 类。这意味着您可以使用 Dependency Properties 而不是实现 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,有很多种实现方式。我更喜欢的是所谓的委托(delegate)命令(有关示例,您可以查看 here )。 我希望这个简短的解释能对您有所帮助。

关于c# - 在用户控件中定义命令绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28111446/

相关文章:

c# - 使用 Json.net 将大量数据流式传输为 JSON 格式

c# - 在 .NET 中捕获存储过程打印输出(不同的模型!)

c# - mvvmcross iOS 绑定(bind)到列表无法将 `lambda expression' 转换为非委托(delegate)类型 `string'

c# - 缺少模型/IdentityModel.cs

c# - 类型 'T' 的值无法转换为

wpf - 与 WPF MVVM Unity、Generic Repository 作斗争

wpf - 有没有办法在 WPF 中合并边距?

wpf - 如何将 Wpf DataGridColumn 绑定(bind)到对象?

c# - 将字符串转换为日期时间的 LINQ 查询

c# - 为什么 yield 具有与常规可枚举不同的行为?