0

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

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

或者换句话说,我是否试图以愚蠢的方式做某事?

看法:

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

2 回答 2

3

首先,我建议公开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();
}

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

于 2012-11-19T17:27:33.393 回答
0

好的。我没有想清楚。

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

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

于 2012-11-19T16:53:39.403 回答