0

考虑到这段代码:

<telerik:RadGridView ItemsSource="{Binding SearchResults}" RowDetailsVisibilityMode="VisibleWhenSelected">
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn Header="First Name" DataMemberBinding="{Binding FirstName}"  />
        <telerik:GridViewDataColumn Header="Last Name" DataMemberBinding="{Binding LastName}"  />
    </telerik:RadGridView.Columns>
    <telerik:RadGridView.RowDetailsTemplate>
        <DataTemplate>
            <Button Command="{Binding OpenView1Command}">Open View 1</Button> <!-- This does not work -->
        </DataTemplate>
    </telerik:RadGridView.RowDetailsTemplate>
</telerik:RadGridView>

OpenView1Command 在我的 ViewModel 中定义。如何使绑定工作?

4

2 回答 2

4

好的,迈克尔,我之前的答案有效,所以 Telerik 没有任何问题,为了证明这一点,我将其编码,复制/粘贴/运行:

XAML:

<Window x:Class="Sample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
    Height="350" Width="525" 
    x:Name="wnd">

<telerik:RadGridView  ItemsSource="{Binding ViewModel.Items, ElementName=wnd}" 
                      AutoGenerateColumns="False"
                      RowDetailsVisibilityMode="VisibleWhenSelected">
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn Header="Name" DataMemberBinding="{Binding Name}"  />
    </telerik:RadGridView.Columns>
    <telerik:RadGridView.RowDetailsTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal"
                        Margin="10,10,10,10">
                <TextBlock Text="This is my RowDetailsTemplate: " />
                <Button Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=ViewModel.SomeCommand}">SomeCommand</Button>
            </StackPanel>
        </DataTemplate>
    </telerik:RadGridView.RowDetailsTemplate>
</telerik:RadGridView>
</Window>

后面的代码:

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using Microsoft.Practices.Prism.Commands;

namespace Sample
{
public partial class MainWindow : Window
{
    public ViewModel ViewModel { get; set; }

    public MainWindow()
    {
        ViewModel = new ViewModel();
        ViewModel.PropertyChanged +=(o,e)=> 
        {
            if(e.PropertyName.Equals("Proof"))
            MessageBox.Show("Button Command Binding just worked!");
        };

        InitializeComponent();

    }
}

public class ViewModel : INotifyPropertyChanged
{
    private ObservableCollection<SomeClass> _items;
    private DelegateCommand _someCmd;
    private bool _proof;

    public ObservableCollection<SomeClass> Items
    {
        get
        {
            if (_items == null)
            {
                _items = new ObservableCollection<SomeClass>();
                for (int i = 0; i < 1000; i++)
                    _items.Add(new    SomeClass(string.Format("name {0}", i)));
            }

            return _items;
        }
    }
    public DelegateCommand SomeCommand
    {
        get
        {
            return _someCmd ?? (_someCmd = new DelegateCommand(() =>
                                                                {
                                                                    Console.WriteLine("Booya, binding works!");
                                                                    Proof = true;
                                                                }));
        }
    }

    public bool Proof
    {
        get { return _proof; }
        set
        {
            _proof = value;
            RaisePropertyChanged("Proof");
        }
    }


    private void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

public class SomeClass : INotifyPropertyChanged
{
    private string _name;

    public SomeClass(string name) { _name = name;}

    public string Name
    {
        get { return _name; }
        set
        {
            if (_name == value)
                return;
            _name = value;
            RaisePropertyChanged("Name");
        }
    }

    private void RaisePropertyChanged(string propertyName)
    {
        if(PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;
}
}
于 2012-04-11T21:45:25.083 回答
1

我不知道您控件的名称,但是这样的东西应该可以工作。基本上找到祖先类型,并挂钩绑定:

  <!--assuming the ViewModel is bound to your view-->
  <UserControl x:Name="YourView"
           xmlns:Views="clr-namespace:YourNameSpace.Views">

             ....
   <DataTemplate>
        <Button Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Views:YourView}}, Path=OpenView1Command}" Open View 1</Button> 
    </DataTemplate>
于 2012-04-11T20:27:37.163 回答