Wpf 将TextBox.TextProperty绑定到模型中绑定类型的属性
我有一个命令按钮列表(带有输入),我想与模型绑定。 问题是我希望按钮中的文本框绑定到某个地方(请参见viewmodel) 以下代码是我尝试过但失败的代码。是否(甚至)可以在模型上设置绑定,然后将其绑定到控件 或者换句话说,我是不是在用愚蠢的方式做事 视图: 视图模型:Wpf 将TextBox.TextProperty绑定到模型中绑定类型的属性,wpf,mvvm,Wpf,Mvvm,我有一个命令按钮列表(带有输入),我想与模型绑定。 问题是我希望按钮中的文本框绑定到某个地方(请参见viewmodel) 以下代码是我尝试过但失败的代码。是否(甚至)可以在模型上设置绑定,然后将其绑定到控件 或者换句话说,我是不是在用愚蠢的方式做事 视图: 视图模型: Commands.Add(new ZoekCommandButtons() { Image = "search.png", IsEnabled = true, **T
Commands.Add(new ZoekCommandButtons()
{
Image = "search.png",
IsEnabled = true,
**Text = new Binding { RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(UserControl), 1), Path = new PropertyPath("FilterText") },**
Command = FilterCommand,
Tooltip = "Zoeken",
Header = "Zoeken"
});
嗯。我没想清楚 将模型中的Text属性更改为string,并使用此属性处理命令
(尽管以某种方式在模型上设置绑定会很好…好。我没想清楚 将模型中的Text属性更改为string,并使用此属性处理命令 (虽然以某种方式在模型上设置绑定会很好…首先,我不建议将
绑定
作为ViewModel属性公开;在这种情况下,我觉得您更像是有嵌套的视图模型,而这种方法更合适——也就是说,您有一个“mamamaviewmodel
”具有“Commands
”属性,它又是“commandbuttonviewmodel
”的集合
好吧,那就是说……你可以这样做,尽管我必须重申,你可能不应该这样做;您缺少的是提供值的“用于评估绑定的内容”。下面是一个课程,让您了解:
public static class BindingEvaluator
{
// need a DP to set the binding to
private static readonly DependencyProperty PlaceholderProperty =
DependencyProperty.RegisterAttached("Placeholder", typeof(object), typeof(DependencyObject), new UIPropertyMetadata(null));
// Evaluate a binding by attaching it to a dummy object/property and evaluating the property value
public static object Evaluate(Binding binding)
{
var throwaway = new DependencyObject();
BindingOperations.SetBinding(throwaway, PlaceholderProperty, binding);
var retVal = throwaway.GetValue(PlaceholderProperty);
return retVal;
}
}
与ViewModel定义相结合,例如:
public class DontDoThisViewModel
{
public Binding TextBinding {get; set;}
public string Text
{
get
{
return BindingEvaluator.Evaluate(TextBinding) as string;
}
}
}
应该可以工作…下面是我在LINQPad中创建的一个测试应用程序:
void Main()
{
var wnd = new Window() { Title = "My window" };
var text = new TextBlock();
text.Text = "Hopefully this shows the window title...";
text.SetBinding(TextBlock.TextProperty, new Binding("Text"));
wnd.Content = text;
var vm = new ViewModel();
var vmBinding = new Binding("Title");
vmBinding.Source = wnd;
vm.TextBinding = vmBinding;
wnd.DataContext = vm;
wnd.Show();
}
再一次,我必须强烈建议你不要这样做……但我很好奇,所以我不得不想出一个办法 首先,我不建议将绑定作为ViewModel属性公开;在这种情况下,我觉得您更像是有嵌套的视图模型,而这种方法更合适——也就是说,您有一个“mamamaviewmodel
”具有“Commands
”属性,它又是“commandbuttonviewmodel
”的集合
好吧,那就是说……你可以这样做,尽管我必须重申,你可能不应该这样做;您缺少的是提供值的“用于评估绑定的内容”。下面是一个课程,让您了解:
public static class BindingEvaluator
{
// need a DP to set the binding to
private static readonly DependencyProperty PlaceholderProperty =
DependencyProperty.RegisterAttached("Placeholder", typeof(object), typeof(DependencyObject), new UIPropertyMetadata(null));
// Evaluate a binding by attaching it to a dummy object/property and evaluating the property value
public static object Evaluate(Binding binding)
{
var throwaway = new DependencyObject();
BindingOperations.SetBinding(throwaway, PlaceholderProperty, binding);
var retVal = throwaway.GetValue(PlaceholderProperty);
return retVal;
}
}
与ViewModel定义相结合,例如:
public class DontDoThisViewModel
{
public Binding TextBinding {get; set;}
public string Text
{
get
{
return BindingEvaluator.Evaluate(TextBinding) as string;
}
}
}
应该可以工作…下面是我在LINQPad中创建的一个测试应用程序:
void Main()
{
var wnd = new Window() { Title = "My window" };
var text = new TextBlock();
text.Text = "Hopefully this shows the window title...";
text.SetBinding(TextBlock.TextProperty, new Binding("Text"));
wnd.Content = text;
var vm = new ViewModel();
var vmBinding = new Binding("Title");
vmBinding.Source = wnd;
vm.TextBinding = vmBinding;
wnd.DataContext = vm;
wnd.Show();
}
再一次,我必须强烈建议你不要这样做……但我很好奇,所以我不得不想出一个办法 哈哈,谢谢你的建议!我确实找到了另一种方法来实现我真正需要的东西,而不是使用绑定进行绑定。不过你确实找到了办法。哈哈,谢谢你的建议!我确实找到了另一种方法来实现我真正需要的东西,而不是使用绑定进行绑定。但是很酷,你确实找到了办法。