我正在开发一个UWP应用程序,我需要在Telerik的RadDataGrid控件中显示数据。在一个场景中,我需要使用TemplatedColumn显示数据并将命令绑定到放置在其DataTemplate内的控件,但是命令不会在ViewModel中被触发,但是当我将事件附加到这些控件时,事件会在后面的代码中被触发。
以下是代码:
<Interactivity:Interaction.Behaviors>
<Core:EventTriggerBehavior EventName="Loaded">
<Core:CallMethodAction MethodName="LoadData"
TargetObject="{Binding}" />
</Core:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>
<Grid x:Name="gdRoot">
<telerikGrid:RadDataGrid ItemsSource="{x:Bind AvailableVM.PickListItems,Mode=OneWay}"
Background="{StaticResource GridLinesBrush}"
SelectionUnit="Cell"
GridLinesBrush="{StaticResource GridLinesBrush}"
AlternateRowBackground="{StaticResource AlternateRowBackground}"
AutoGenerateColumns="False"
ScrollViewer.VerticalScrollBarVisibility="Hidden">
<telerikGrid:RadDataGrid.Columns>
<telerikGrid:DataGridTemplateColumn Header="Assign"
SizeMode="Auto">
<telerikGrid:DataGridTemplateColumn.CellContentTemplate>
<DataTemplate>
<Button Background="Transparent"
Command="{Binding DataContext.ListSelectedCommand, ElementName=gdRoot}"/>
</DataTemplate>
</telerikGrid:DataGridTemplateColumn.CellContentTemplate>
</telerikGrid:DataGridTemplateColumn>
</telerikGrid:RadDataGrid.Columns>
</telerikGrid:RadDataGrid>
</Grid>
这是ViewModel代码:
private ICommand _listSelectedCommand;
public ICommand ListSelectedCommand
{
get { return _listSelectedCommand; }
set { Set(nameof(ListSelectedCommand), ref _listSelectedCommand,value); }
}
public void LoadData()
{
InitializeCommands();
}
private void InitializeCommands()
{
ListSelectedCommand= new RelayCommand(()=>
{
});
}
这可能是造成这种情况的原因。
答案 0 :(得分:0)
该命令最可能的原因不起作用应该是您没有正确绑定命令。由于您的代码段不完整,因此错误的绑定可能由多种原因引起。这是我测试过的一个小型演示,可以参考我的工作。
XAML:
<telerikGrid:RadDataGrid ItemsSource="{x:Bind AvailableVM.PickListItems,Mode=OneWay}"
Background="White"
SelectionUnit="Cell"
GridLinesBrush="Pink"
AlternateRowBackground="Azure"
AutoGenerateColumns="False"
ScrollViewer.VerticalScrollBarVisibility="Hidden"
x:Name="radgrid">
<telerikGrid:RadDataGrid.Columns>
<telerikGrid:DataGridTextColumn PropertyName="Country"/>
<telerikGrid:DataGridTextColumn PropertyName="City"/>
<telerikGrid:DataGridTemplateColumn Header="Assign" SizeMode="Auto">
<telerikGrid:DataGridTemplateColumn.CellContentTemplate>
<DataTemplate x:DataType="local:DataTest">
<Button Background="Transparent" Command="{x:Bind ListSelectedCommand }" Content="command testing" />
</DataTemplate>
</telerikGrid:DataGridTemplateColumn.CellContentTemplate>
</telerikGrid:DataGridTemplateColumn>
</telerikGrid:RadDataGrid.Columns>
</telerikGrid:RadDataGrid>
代码背后:
public ViewModel AvailableVM { get; set; }
public MainPage()
{
this.InitializeComponent();
AvailableVM = new ViewModel();
}
public class ViewModel
{
public void Testmethod()
{
}
public ObservableCollection<DataTest> PickListItems { get; set; }
public ViewModel()
{
PickListItems = new ObservableCollection<DataTest>()
{
new DataTest { Country = "Brazil", City = "Caxias do Sul", ListSelectedCommand = new RelayCommand(()=>{ })},
new DataTest { Country = "Ghana", City = "Wa", ListSelectedCommand = new RelayCommand(Testmethod)},
new DataTest { Country = "Brazil", City = "Fortaleza"}
};
}
}
public class DataTest
{
public string City { get; set; }
public string Country { get; set; }
public ICommand ListSelectedCommand { get; set; }
}
class RelayCommand : ICommand
{
public event EventHandler CanExecuteChanged;
private Action _action;
public RelayCommand(Action action)
{
this._action = action;
}
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
this._action();
}
}
顺便说一句,CellContentTemplate
可能会对绑定产生影响。试图以上述方式绑定。另外RadDataGrid
有自己的This tutorial,如果你的情景适合,你可以参考。