2017-10-16 153 views
0

我正在開發一個UWP應用程序,我需要在Telerik的RadDataGrid控件中顯示數據。在一個場景中,我需要使用TemplatedColumn顯示數據並將命令綁​​定到DataTemplate內部的控件,但命令不會在ViewModel中觸發,但是當我將事件附加到這些控件時,會在代碼後面觸發事件。RadDataGrid中的TemplatedColumn中的綁定命令UWP

下面是代碼:

<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> 

這裏是視圖模型代碼:

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

該命令的最可能的原因不起作用應該是你沒有正確綁定命令。由於你的代碼片段不是完整的,不正確的綁定可能是由許多原因造成的。這裏是我測試過的一個小型演示,可以在您參考的一面工作。

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有自己的commands如果有一個適合你的場景,你可以參考。

+0

謝謝@Sunteen。我發現這背後的原因。這裏是Link https://www.telerik.com/forums/what-is-the-correct-way-to-bind-a-command-on-button-in-template。 –