考虑此代码:
<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中定义。如何使绑定工作?
答案 0 :(得分:4)
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;
}
}
答案 1 :(得分:1)
我不知道你控件的名字,但这样的东西应该有效。基本上找到anscestor类型,并挂钩绑定:
<!--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>