Binding Selecteditems Datagrid

2019-08-30 01:21发布

So on my WPF I enabled a right click functionality. If the person in the datagrid is rightclicked then you can choose email and it'll email that person. But Now I want to improve that by having a Multiple select option available. I'm wondering what's wrong with my binding or am I taking the wrong binding approach.

Code for Single/SelectedItem

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

xaml side: Binding 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>

Which works! This is my attempt below to Upgrade to multi select 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));
    }
}

标签: c# wpf
1条回答
爷的心禁止访问
2楼-- · 2019-08-30 02:11

Assuming that your RadGridView is inside a ListView, then you need an IList property to data bind to the ListView.SelectedItems property.

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

...

<ListView ItemSource="{Binding Items}" SelectedItems="{Binding SelectYN}" ... />
查看更多
登录 后发表回答