C# 代码隐藏中的数据绑定拒绝工作

C# 代码隐藏中的数据绑定拒绝工作,c#,wpf,C#,Wpf,我试图在代码隐藏中设置数据绑定,但它不想工作 当我在XAML中这样做时: <Label x:Name="lblSelectedItem" Margin="0,0,5,5" DockPanel.Dock="Left" Content="{Binding (Canvas.Left),ElementName=Ming}"></Label> var X1Binding = new Binding("Canvas.Left") { ElementName="Ming"}; Bin

我试图在代码隐藏中设置数据绑定,但它不想工作

当我在XAML中这样做时:

<Label x:Name="lblSelectedItem" Margin="0,0,5,5" DockPanel.Dock="Left" Content="{Binding (Canvas.Left),ElementName=Ming}"></Label>
var X1Binding = new Binding("Canvas.Left") { ElementName="Ming"};
BindingOperations.SetBinding(lblSelectedItem, ContentProperty, X1Binding);
它没有任何价值


如何正确执行此操作?

必须指定多个绑定属性值

var X1Binding = new Binding("Canvas.Left") { ElementName="Ming"};
//X1Binding.Source = ViewModel; // Well the canvas is not on the view model so default value is the datacontext of the view.
X1Binding.Mode = BindingMode.TwoWay;
X1Binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
BindingOperations.SetBinding(lblSelectedItem, ContentProperty, X1Binding);

尝试设置绑定和属性路径,如下所示:

Binding binding = new Binding();
binding.ElementName = "Ming";
binding.Path = new PropertyPath(Canvas.LeftProperty);
lblSelectedItem.SetBinding(ContentControl.ContentProperty, binding);

在画布周围使用括号。左如下:

var X1Binding = new Binding("(Canvas.Left)") { ElementName = "Rect" };
BindingOperations.SetBinding(Lbl1, Label.ContentProperty, X1Binding);

内容
绑定只是单向的(从源到目标)。指定
BindingMode.TwoWay
UpdateSourceTrigger.PropertyChanged
在这里没有任何意义。非常感谢:-)