【发布时间】:2018-07-17 14:26:03
【问题描述】:
为了更动态地创建我的 GUI,我喜欢在 XAML 中执行我在代码中定义的绑定:
编辑: 我不想在代码中调用 SetBinding()。我想在 XAML 中设置绑定。
代码:
public class SPSProperty
{
public string LabelContent { get; private set; }
public string PropertyPath { get; private set; }
public Binding Binding { get; private set; }
public SPSProperty (INotifyPropertyChanged viewModel,string propertyPath, string labelContent)
{
LabelContent = labelContent;
PropertyPath = propertyPath;
Binding = new Binding(propertyPath);
Binding.Source = viewModel;
}
}
视图模型:
public class MainWindowViewModel:BindableBase
{
public SPSProperty Property { get; set; }
public MainWindowViewModel()
{
Property = new SPSProperty(this, "Test_Property", "Test Property");
}
private string _Test_Property;
public string Test_Property
{
get { return _Test_Property; }
set { SetProperty(ref _Test_Property, value); }
}
}
如何在 XAML 中使用绑定?
TextBox Text="{Binding Property.Binding}"
【问题讨论】:
-
这行得通吗?
TextBox DataContext="{Binding Path=Property}" Text="{Binding Path=Binding}" -
@ViRuSTriNiTy 它确实可以正常工作,我喜欢在最后设置 Test_Property。