1

有没有办法绑定到 C# 中的依赖属性(就像 XAML 一样)?

我知道我可以做更改通知,但我希望有一种方法来进行“双向”绑定。(因此更改我的值会更改依赖项属性。)

例子:

在我的用户控制视图中

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

2 回答 2

1

您可以在 C# 中执行此操作 - 您必须手动构建绑定:

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

有关详细信息,请参阅如何:在代码中创建绑定

于 2013-02-04T17:02:06.467 回答
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>

我希望这将有所帮助。

于 2013-02-04T17:16:23.443 回答