【问题标题】:Bind TextBox.TextProperty to a property of type Binding from the Model将 TextBox.TextProperty 绑定到模型中的 Binding 类型的属性
【发布时间】:2012-11-20 01:27:33
【问题描述】:

我有一个命令按钮列表(带输入),我想与模型绑定。 问题是我希望按钮中的文本框绑定到某个地方(请参阅视图模型)。

以下代码是我尝试过但失败的代码。是否可以(甚至)在模型上设置绑定,然后将其绑定到控件?

或者换句话说,我想以愚蠢的方式做某事?

查看:

<ToolBar Grid.Row="1" Grid.ColumnSpan="2" Grid.Column="0" ItemsSource="{Binding SelectedTab.Commands}" Height="34">
    <ToolBar.Resources>
        <DataTemplate DataType="{x:Type model:ZoekCommandButtons}">
            <Button Command="{Binding Command}" ToolTip="{Binding Tooltip}" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}">
                <StackPanel Orientation="Horizontal">
                    <Image Source="{Binding Image, Converter={StaticResource ImageConv}}" Height="16" Width="16"></Image>
                    **<TextBox Width="100" Text="{Binding Text}">**
                        <TextBox.InputBindings>
                            <KeyBinding Gesture="Enter" Command="{Binding Command}"></KeyBinding>
                        </TextBox.InputBindings>
                    </TextBox>
                </StackPanel>
            </Button>
        </DataTemplate>
    </ToolBar.Resources>
</ToolBar>

型号:

    public class ZoekCommandButtons : BaseModel, ICommandItem
    {
        private string _header;
        private string _image;
        private bool _isEnabled;
        private Visibility _isVisible;
        private ICommand _command;
        private string _tooltip;
        private Binding _text;


        public Binding Text
        {
            get { return _text; }
            set { _text = value; OnPropertyChanged("Text"); }
        }
(etc)

视图模型:

    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"
    });

【问题讨论】:

    标签: wpf mvvm


    【解决方案1】:

    首先,我建议将Binding 公开为 ViewModel 属性;在这种特殊情况下,对我来说听起来更像是您嵌套了 ViewModel,并且这种方法会更合适 - 也就是说,您有一个“MamaViewModel”,它具有您的“Commands”属性,而这又是“CommandButtonViewModels”的集合...

    好的,也就是说……你可以这样做,虽然我必须重申你可能不应该;您缺少的是“评估绑定的东西”以提供值。这是一个可以为您提供的课程:

    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();
    }
    

    再次,我必须强烈建议您不要这样做......但我很好奇,所以我必须想出一个方法。 ;)

    【讨论】:

    • 哈哈谢谢你的建议!我确实找到了另一种方法来完成我真正需要的事情,它不是使用绑定进行绑定。但很酷,你确实找到了方法。
    【解决方案2】:

    好的。我没有想清楚。

    将模型中的文本属性更改为字符串,并使用此属性处理命令。

    (尽管以某种方式在模型上设置绑定会很好......)

    【讨论】:

      猜你喜欢
      • 2017-05-13
      • 1970-01-01
      • 1970-01-01
      • 2020-10-24
      • 1970-01-01
      • 1970-01-01
      • 2013-05-01
      • 1970-01-01
      • 2015-01-23
      相关资源
      最近更新 更多