好的,迈克尔,我之前的答案有效,所以 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;
}
}