2013-02-04 4 views
1

XAML과 마찬가지로 C#의 종속성 속성에 바인딩하는 방법이 있습니까?코드에서 종속성 속성에 바인딩 (변경 알림 외)

내가 변경 알림을 할 수 있다는 것을 알고 있지만 "양방향"바인딩을 수행 할 수있는 방법이 필요했습니다. (내 값을 변경하면 종속성 속성을 변경하도록.)

예 :

내보기 모델에서

public static readonly DependencyProperty IsRequiredProperty = 
    DependencyProperty.Register("IsRequired", typeof(bool), 
     typeof(MyUserControl), new FrameworkPropertyMetadata(default(bool))); 

public bool IsRequired 
{ 
    get { return (bool)GetValue(IsRequiredProperty); } 
    set { SetValue(IsRequiredProperty, value); } 
} 

내 사용자 컨트롤보기에서

:

// This is the one I want bound to the dependency property. 
bool IsRequired { //INotifyPropertyChanged getter and setter} 

public void SomeCommandExec(Object obj) 
{ 
    // Update the dependency property by doing this: 
    IsEnabled = False; 
} 

답변

1

당신은이 작업을 수행 할 수 있습니다 C#에서는 - 수동으로 Binding을 빌드해야합니다.

// You need these instances 
var yourViewModel = GetTheViewModel(); 
var yourView = GetYourView(); 

Binding binding = new Binding("IsRequired"); 
binding.Source = yourViewModel; 
binding.Mode = BindingMode.TwoWay; 
yourView.SetBinding(YourViewType.IsRequiredProperty, binding); 

자세한 내용은 How To: Create a Binding in Code을 참조하십시오.

1

안녕하세요이 도움이 될 희망이

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     DataContext = new ViewModel(); 
     Binding binding = new Binding("IsRequired") 
     { 
      Source = UserControl1.IsRequiredProperty, 
      Mode = BindingMode.TwoWay 
     }; 
    } 
} 

public class ViewModel : INotifyPropertyChanged 
{ 
    private bool isRequired; 
    public bool IsRequired 
    { 
     get { return isRequired; } 
     set { isRequired = value; Notify("IsRequired"); } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    private void Notify(string propName) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propName)); 
    } 

    private CommandHandler mycommand; 
    public CommandHandler MyCommand { get { return mycommand ?? (mycommand = new CommandHandler((obj) => OnAction(obj))); } } 

    private void OnAction(object obj) 
    { 
     IsRequired = true; 
    } 

} 

public class CommandHandler : ICommand 
{ 
    public CommandHandler(Action<object> action) 
    { 
     action1 = action; 
    } 
    Action<object> action1; 
    public bool CanExecute(object parameter) 
    { 
     return true; 
    } 

    public event EventHandler CanExecuteChanged; 

    public void Execute(object parameter) 
    { 
     action1(parameter); 
    } 
} 

public partial class UserControl1 : UserControl 
{ 
    public UserControl1() 
    { 
     InitializeComponent(); 
    } 
    public static readonly DependencyProperty IsRequiredProperty = DependencyProperty.Register("IsRequired", typeof(bool), typeof(UserControl1), new FrameworkPropertyMetadata(default(bool))); 

    public bool IsRequired 
    { 
     get { return (bool)GetValue(IsRequiredProperty); } 
     set { SetValue(IsRequiredProperty, value); } 
    } 
} 

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*"/> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 
    <local:UserControl1></local:UserControl1> 
    <Button Command="{Binding MyCommand}" Grid.Row="1" Content="Action"/> 
</Grid> 

같은 것을보십시오.

관련 문제