Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 多选列表框绑定问题_C#_Wpf_Xaml_Listbox_Wpf Controls - Fatal编程技术网

C# 多选列表框绑定问题

C# 多选列表框绑定问题,c#,wpf,xaml,listbox,wpf-controls,C#,Wpf,Xaml,Listbox,Wpf Controls,我已经创建了一个从列表框派生的自定义控件,在获取“SelectedItemsList”绑定到视图模型中它的相应属性时遇到了问题 问题是:列表框中的选定项未进入视图模型中绑定到的特性。列表框允许进行多个选择,但在视图模型的列表中没有选择 多选择列表框: public class MultiSelectListBox : ListBox { public MultiSelectListBox() { } public static readonly DependencyProper

我已经创建了一个从列表框派生的自定义控件,在获取“SelectedItemsList”绑定到视图模型中它的相应属性时遇到了问题

问题是:列表框中的选定项未进入视图模型中绑定到的特性。列表框允许进行多个选择,但在视图模型的列表中没有选择

多选择列表框:

public class MultiSelectListBox : ListBox
{
    public MultiSelectListBox() { }

    public static readonly DependencyProperty SelectedItemsListProperty =
        DependencyProperty.Register(
            "SelectedItemsList", 
            typeof(IList), 
            typeof(MultiSelectListBox),
            new PropertyMetadata(default(IList)));
    public IList SelectedItemsList
    {
        get { return (IList) GetValue(SelectedItemsListProperty); }
        set { SetValue(SelectedItemsListProperty, value); }
    }
}
MainWindow.xaml中的声明:

<local:MultiSelectListBox
    DataContext="{StaticResource viewModel}"
    DockPanel.Dock="Left"
    Visibility="{Binding IsThailandFinal, Converter={StaticResource BoolToVisConverter}, FallbackValue=Visible}"
    ItemsSource="{Binding SelectedOutputtapeList}"
    SelectionMode="Multiple"
    SelectedItemsList="{Binding SelectedOutputTapes, Mode=TwoWay}"
    HorizontalAlignment="Right"
    Background="DeepSkyBlue"
    Foreground="MidnightBlue"
    ScrollViewer.VerticalScrollBarVisibility="Visible"
    Height="100"
    Width="70"
    Margin="5"/>

视图模型(简化):

公共类BTLogFrontEndViewModel:ViewModelBase
{
私有列表选择Outputtapes;
公共BTLogFrontEndViewModel()
{
selectedOutputTapes=新列表();
}
公共列表已选择Outputtapes
{
得到
{
返回所选的outputtapes;
}
设置
{
选择OutPuttapes=值;
已更改的房地产(“已选择的OutPuttapes”);
}
}
}

一种方法是不使用自定义列表框,而是使用列表中扩展INotifyPropertyChanged的对象:

<ListBox
    Width="70"
    Height="100"
    HorizontalAlignment="Right"
    Margin="5"
    Background="DeepSkyBlue"
    DockPanel.Dock="Left"
    Foreground="MidnightBlue"
    ItemsSource="{Binding SelectedOutputtapeList}"
    ScrollViewer.VerticalScrollBarVisibility="Visible"
    SelectionMode="Multiple"
    DisplayMemberPath="Description"
    Visibility="{Binding IsThailandFinal, Converter={StaticResource BoolToVisConverter}, FallbackValue=Visible}">
    <ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}"/>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>
需要注意的重要事项是在listbox上,我添加了DisplayMemberPath属性,以使用OutputApeViewModel类中的description字段来显示listbox上的内容。它还有一个项目容器样式,当在列表框中选择时,该样式绑定到OutputApeViewModel.IsSelected属性。此OutputTapeViewModel.IsSelected允许您以以下方式使用BTLogFrontEndViewModel.SelectedOutputtapeList属性:

var selectedItems = SelectedOutputtapeList.Where(item => item.IsSelected);

这仅在您不关心执行LINQ表达式的开销的情况下才有效。

为您的ObservableCollection发布代码,并且您在Visual Studio的输出窗口中是否看到任何绑定错误?ObservableCollection是一个.NET集合,没有要显示的代码。我的意思是声明
SelectedOutputTapes
,不是ObservableCollection的源代码。另外,您所说的“我有问题”是什么意思?关于问题陈述,你能说得更准确些吗?罗希特,我已经更新了我的问题的更多细节。谢谢你迄今为止的帮助,我很感激。
public class BTLogFrontEndViewModel : ViewModelBase
{
    private ObservableCollection<OutputTapeViewModel> m_SelectedOutputtapeList;

    public ObservableCollection<OutputTapeViewModel> SelectedOutputtapeList
    {
        get
        {
            return m_SelectedOutputtapeList;
        }
        set
        {
           m_SelectedOutputtapeList = value;

           NotifyPropertyChanged("SelectedOutputtapeList");
        }
    }
}
public class OutputTapeViewModel : ViewModelBase
{
    private string m_Description;

    public string Description
    {
        get
        {
            return m_Description;
        }
        set
        {
            m_Description = value;

            NotifyPropertyChanged("Description");
        }
    }

    private bool m_IsSelected;

    public string IsSelected
    {
        get
        {
            return m_IsSelected;
        }
        set
        {
            m_IsSelected = value;

            NotifyPropertyChanged("IsSelected");
        }
    }
}
var selectedItems = SelectedOutputtapeList.Where(item => item.IsSelected);