绑定Selecteditems Datagrid

时间:2014-03-13 21:11:06

标签: c# wpf

所以在我的WPF上我启用了右键单击功能。如果右键单击数据网格中的人员,则可以选择email,然后通过电子邮件向该人发送电子邮件。但现在我希望通过提供Multiple select选项来改善这一点。我想知道我的绑定有什么问题,或者我采取了错误的绑定方法。

Single / SelectedItem的代码

public void SendEmail()
{
    var vm = new EmailViewModel(Events);
    vm.ByIt(SelectedItem.Id);
    }
}

xaml side:绑定SelectedItem

<telerik:RadGridView ItemsSource="{Binding Items, IsAsync=True}" SelectedItem="{Binding SelectedItem}">
<telerik:RadGridView.ContextMenu>
     <ContextMenu>
          <MenuItem Header="Email" cal:Message.Attach="[Click] = [SendEmail()]"/>
     </ContextMenu>
</telerik:RadGridView.ContextMenu>

哪个有效! 这是我在Upgrade以下尝试多选Binding

的尝试
<telerik:RadGridView.ItemContainerStyle>
      <Style TargetType="{x:Type telerik:GridViewRow}">
           <Setter Property="IsSelected" Value="{Binding Mode=OneWayToSource, Path=SelectYN}"></Setter>
      </Style>
</telerik:RadGridView.ItemContainerStyle>

private BindableCollection<PersonDTO> selectYN;
public BindableCollection<PersonDTO> SelectYN
{
    get { return selectYN; }
    set
    {
        if (value != selectYN)
        {
            selectYN = value;
            NotifyOfPropertyChange(() => SelectYN);
        }
     }
}

public void SendEmail()
{
    foreach (PersonDTO value in SelectYN)
    {
        var vm = new EmailViewModel(Events);
        vm.ById(value.Id);
        Events.PublishOnUIThread(new ShowTabEvent(vm));
    }
}

1 个答案:

答案 0 :(得分:0)

假设您的RadGridView位于ListView内,那么您需要IList属性才能将数据绑定到ListView.SelectedItems属性。

private IList selectYN;
public IList SelectYN
{
    get { return selectYN; }
    set
    {
        if (value != selectYN)
        {
            selectYN = value;
            NotifyOfPropertyChange(() => SelectYN);
        }
     }
}

...

<ListView ItemSource="{Binding Items}" SelectedItems="{Binding SelectYN}" ... />