我有一个dialogBox填充网格行,我想在ViewModel的单选按钮选择中选择特定的网格行数据
这是我的Xaml
<cc:DialogBase xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class="LMS.Client.View.CampaignSearchResultsDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:cc="clr-namespace:LMS.Client.View;assembly=LMS.Client.Common"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400"
xmlns:attach="clr-namespace:LMS.Client.Model.ViewModels.AttachedProperties;assembly=LMS.Client.Model"
xmlns:converter="clr-namespace:LMS.Client.Model.ViewModels.Converters;assembly=LMS.Client.Model"
xmlns:model="clr-namespace:LMS.Client.Model;assembly=LMS.Client.Common">
<cc:DialogBase.Resources>
<converter:DataContextFinderFromControl x:Key="dataContextFinder"/>
<model:ViewModelSource Source="{Binding}" x:Name="viewModel"/>
</cc:DialogBase.Resources>
<Grid x:Name="LayoutRoot" Background="White" Height="300" Width="600">
<sdk:DataGrid AutoGenerateColumns="False" Grid.Column="0" Grid.Row="0"
ItemsSource="{Binding Path=Leads}"
SelectedItem="{Binding Path=SelectedLead,Mode=TwoWay}" Name="dgSearchResult"
Style="{StaticResource DataGridStyle}">
<sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn Header="Select">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<RadioButton Content="Test" Style="{StaticResource SemiRadioButtonStyle}" GroupName="Lead"
IsEnabled="True" Command="{Binding Path= CheckedCommand}" IsChecked="{Binding InboundChecked,Mode=TwoWay}"/>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid>
<StackPanel Width="300" Height="50" Background="White">
<Grid Height="50">
</Grid>
</StackPanel>
</Grid>
</cc:DialogBase>
和ViewModel
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using LMS.Server.DataAccess;
using System.Collections.ObjectModel;
using LMS.Client.Commanding;
using System.ComponentModel;
namespace LMS.Client.Model
{
public class CampaignSearchResultsViewModel : ViewModelBase
{
public CampaignSearchResultsViewModel(List<Lead> obj)
{
foreach(Lead lead in obj)
{
SelectedLead = lead;
}
}
public CampaignSearchResultsViewModel()
{
this.Commands.Add("CheckedCommand", new ActionCommand<Lead>(CheckIt));
_leads = new ObservableCollection<Lead>();
// this.icv=System.Windows.Data.CollectionViewSource.SourceProperty(_leads);
//_leads.Add(new Lead { FirstName = "Neeraj", LastName = "Verma" });
//_leads.Add(new Lead { FirstName = "Tarun", LastName = "Singh" });
}
private void CheckIt(Lead lead)
{
SelectedLead = lead;
LeadViewModel lmv = new LeadViewModel(this);
var x = SelectedLead;
CampaignSearchResultsViewModel vm = new CampaignSearchResultsViewModel();
}
#region Private
private ObservableCollection<Lead> _leads;
public bool IsChecked { get; set; }
private ICommand _checkedCommand;
private object _testProperty;
private Lead _selectedLead;
private ICollectionView icv;
#endregion
private ICommand _checkedRadioCommand;
private bool _inboundChecked;
#region Properties
public ObservableCollection<Lead> Leads
{
get { return _leads; }
set
{
_leads = value;
FirePropertyChanged("Leads");
}
}
public Lead SelectedLead
{
get { return _selectedLead; }
set { _selectedLead = value; }
}
public ICommand CheckedCommand
{
get
{
return Commands["CheckedCommand"];
}
}
public bool InboundChecked
{
get
{
return _inboundChecked;
}
private set
{
if (_inboundChecked != value)
{
_inboundChecked = value;
FirePropertyChanged("InboundChecked");
}
}
}
#endregion
}
}
我无法在单选按钮上的网格中映射数据,请告诉我我在哪里缺少
答案 0 :(得分:0)
添加CommandParameter =“{Binding}”,以便您可以在视图模型中获取整个对象(Lead)。然后,您可以将此对象设置为选定项。所以你可以修改你的代码,如
<RadioButton Content="Test" Style="{StaticResource SemiRadioButtonStyle}" GroupName="Lead"
IsEnabled="True" Command="{Binding Path= CheckedCommand}" CommandParameter="{Binding}" IsChecked="{Binding InboundChecked,Mode=TwoWay}"/>
然后在您的代码中,您可以将CommandParameter强制转换为Lead并将此对象设置为SelectedLead