简体   繁体   中英

Binding Selecteditems Datagrid

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));
    }
}

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}" ... />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM