2015-01-23 7 views
1

두 개의 버튼과 하나의 확인란을 사용하여 사용자 정의 컨트롤을 작성 했으므로 각 버튼과 확인란마다 명령을 데이터 컨텍스트에 바인딩하려고합니다. 하지만 명령 바인딩을 정의하는 방법을 모르겠습니다. 사용자 콘트롤에 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? 모든 ChannelSetupControl 클래스의

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

답변

2

먼저 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을 볼 수있다). 이 간단한 설명이 도움이되기를 바랍니다.

관련 문제