Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.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_Binding_Listbox - Fatal编程技术网

C# 在新列表中仅显示列表框中的选定项

C# 在新列表中仅显示列表框中的选定项,c#,wpf,xaml,binding,listbox,C#,Wpf,Xaml,Binding,Listbox,我对WPF和XAML比较陌生 我在XAML中有一个ListBox Name=“EmployeeTitles”(如下所示)。为了演示问题DataContext=employees,它是observedcollection 我想在不同的控件中显示所选项目。例如,有一个只显示选定项目的列表。我是否可以在XAML中标记我只想显示带有IsChecked=“True”的项目?我知道我可以让另一个ObservableCollection只存储选定的项,并在属性被选中时在“代码隐藏”中更新它,但这似乎是一种开销

我对WPF和XAML比较陌生

我在XAML中有一个
ListBox Name=“EmployeeTitles”
(如下所示)。为了演示问题
DataContext=employees
,它是
observedcollection


我想在不同的控件中显示所选项目。例如,有一个只显示选定项目的列表。我是否可以在XAML中标记我只想显示带有
IsChecked=“True”
的项目?我知道我可以让另一个ObservableCollection只存储选定的项,并在属性被选中时在“代码隐藏”中更新它,但这似乎是一种开销,我想应该有一种方法在XAML中实现它

您可以将另一个列表框与原始列表框的selecteditems绑定。请尝试下面的代码

<Grid>
    <StackPanel Orientation="Horizontal">
        <ListBox x:Name="Emp" ItemsSource="{Binding EmpCollection}" SelectionMode="Extended">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <ToggleButton IsChecked="{Binding IsSelected, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}">
                            <!--StackPanel will have more items-->
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding Title}"/>
                            </StackPanel>
                        </ToggleButton>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <ListBox x:Name="SelectedEmp" ItemsSource="{Binding ElementName=Emp,Path=SelectedItems}" DisplayMemberPath="Title"/>
    </StackPanel>
</Grid>

公共部分类窗口2:窗口
{
公共窗口2()
{
初始化组件();
this.DataContext=newviewmodel();
}
}
类视图模型
{
公共ObservableCollection EmpCollection{get;set;}
公共视图模型()
{
EmpCollection=新的ObservableCollection();
对于(int i=0;i<10;i++)
{
添加(新的Emp(){Title=“Title”+i});
}
}
}
Emp类:INotifyPropertyChanged
{
私有字符串\u标题;
公共字符串标题
{
获取{return\u title;}
设置
{
_标题=价值;
OnPropertyChanged();
}
}
私立学校当选;
公选学校
{
得到
{
返回(未选);;
}
设置
{
_isSelected=值;
OnPropertyChanged();
}
}
公共事件属性更改事件处理程序属性更改;
[NotifyPropertyChangedInvocator]
受保护的虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
{
PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(propertyName));
}
}

您可以将另一个列表框与原始列表框的selecteditems绑定。请尝试下面的代码

<Grid>
    <StackPanel Orientation="Horizontal">
        <ListBox x:Name="Emp" ItemsSource="{Binding EmpCollection}" SelectionMode="Extended">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <ToggleButton IsChecked="{Binding IsSelected, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}">
                            <!--StackPanel will have more items-->
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding Title}"/>
                            </StackPanel>
                        </ToggleButton>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <ListBox x:Name="SelectedEmp" ItemsSource="{Binding ElementName=Emp,Path=SelectedItems}" DisplayMemberPath="Title"/>
    </StackPanel>
</Grid>

公共部分类窗口2:窗口
{
公共窗口2()
{
初始化组件();
this.DataContext=newviewmodel();
}
}
类视图模型
{
公共ObservableCollection EmpCollection{get;set;}
公共视图模型()
{
EmpCollection=新的ObservableCollection();
对于(int i=0;i<10;i++)
{
添加(新的Emp(){Title=“Title”+i});
}
}
}
Emp类:INotifyPropertyChanged
{
私有字符串\u标题;
公共字符串标题
{
获取{return\u title;}
设置
{
_标题=价值;
OnPropertyChanged();
}
}
私立学校当选;
公选学校
{
得到
{
返回(未选);;
}
设置
{
_isSelected=值;
OnPropertyChanged();
}
}
公共事件属性更改事件处理程序属性更改;
[NotifyPropertyChangedInvocator]
受保护的虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
{
PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(propertyName));
}
}

您无法绑定
列表框。请选择editems
。我已更正。那很酷。不过,您仍然需要以某种方式将它们返回到viewmodel。您无法绑定
列表框。请选择EditEMS
。我可以更正。那很酷。不过,您仍然需要以某种方式将它们返回到viewmodel。
<Grid>
    <StackPanel Orientation="Horizontal">
        <ListBox x:Name="Emp" ItemsSource="{Binding EmpCollection}" SelectionMode="Extended">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <ToggleButton IsChecked="{Binding IsSelected, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}">
                            <!--StackPanel will have more items-->
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding Title}"/>
                            </StackPanel>
                        </ToggleButton>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <ListBox x:Name="SelectedEmp" ItemsSource="{Binding ElementName=Emp,Path=SelectedItems}" DisplayMemberPath="Title"/>
    </StackPanel>
</Grid>
public partial class Window2 : Window
{
    public Window2()
    {
        InitializeComponent();
        this.DataContext = new ViewModel();
    }
}


class ViewModel
{
    public ObservableCollection<Emp> EmpCollection { get; set; }

    public ViewModel()
    {
        EmpCollection = new ObservableCollection<Emp>();
        for (int i = 0; i < 10; i++)
        {
            EmpCollection.Add(new Emp() {Title = "Title"+i});
        }
    }

}

class Emp:INotifyPropertyChanged
{
    private string _title;
    public string Title
    {
        get { return _title; }
        set
        {
            _title = value;
            OnPropertyChanged();
        }
    }

    private bool _isSelected;
    public bool IsSelected
    {
        get
        {
            return _isSelected;
        }
        set
        {
            _isSelected = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

    }
}