使用多选模式ListBox绑定项目?

时间:2013-11-04 18:23:55

标签: c# wpf listbox

这是我需要解决的问题。

我的数据内容在DemoList类中:

注意: DemoHeader对象包含DemoItem对象的ObservableCollection,而DemoList对象包含DemoHeader对象的ObservableCollection

public enum Type { article, product, material }

public class DemoHeader
{
    private ObservableCollection<DemoItem> _items;

    public ObservableCollection<DemoItem> Items
    {
        get { return _items; }
        set { _items = value; }
    }

    public DemoHeader(string document)
    {
        this._salesOrder = document;
        _items = new ObservableCollection<DemoItem>();
    }

    private string _salesOrder;

    public string SalesOrder
    {
        get { return _salesOrder; }
        set { _salesOrder = value; }
    }
}

public class DemoItem
{
    public DemoItem(string name, Type type)
    {
        this._name = name;
        this._type = type;
    }

    private Type _type;

    public Type Type
    {
        get { return _type; }
        set { _type = value; }
    }

    private string _name;

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }
}


public class DemoList : ObservableCollection<DemoHeader>//, ICollectionView
{
    public DemoList() 
    {
        DemoHeader dd = new DemoHeader("Doc-1");
        dd.Items.Add(new DemoItem("T-1", Type.article));
        dd.Items.Add(new DemoItem("M-1", Type.material));

        DemoHeader dd2 = new DemoHeader("Doc-2");
        dd2.Items.Add(new DemoItem("P-1", Type.product));
        dd2.Items.Add(new DemoItem("P-2", Type.product));

        this.Add(dd);
        this.Add(dd2);

    }
}

public class DemoHeader { private ObservableCollection<DemoItem> _items; public ObservableCollection<DemoItem> Items { get { return _items; } set { _items = value; } } public DemoHeader(string document) { this._salesOrder = document; _items = new ObservableCollection<DemoItem>(); } private string _salesOrder; public string SalesOrder { get { return _salesOrder; } set { _salesOrder = value; } } } public class DemoItem { public DemoItem(string name, Type type) { this._name = name; this._type = type; } private Type _type; public Type Type { get { return _type; } set { _type = value; } } private string _name; public string Name { get { return _name; } set { _name = value; } } } public class DemoList : ObservableCollection<DemoHeader>//, ICollectionView { public DemoList() { DemoHeader dd = new DemoHeader("Doc-1"); dd.Items.Add(new DemoItem("T-1", Type.article)); dd.Items.Add(new DemoItem("M-1", Type.material)); DemoHeader dd2 = new DemoHeader("Doc-2"); dd2.Items.Add(new DemoItem("P-1", Type.product)); dd2.Items.Add(new DemoItem("P-2", Type.product)); this.Add(dd); this.Add(dd2); } }

XAML:

注意:每个ListBox都有4个CollectionViewSource。

<Window x:Class="WuZet.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:c="clr-namespace:WuZet"
        Title="WuZet" WindowStartupLocation="CenterScreen" ResizeMode="CanResize" Loaded="window_loaded" Background="#ECE9D8" WindowStyle="ToolWindow" Icon="/WuZet;component/Images/ksi_ikona.ico" Topmost="True" WindowState="Maximized" SizeToContent="WidthAndHeight"> 
    <Window.Resources>
        <CollectionViewSource x:Key="list" Source="{Binding}"></CollectionViewSource>
        <CollectionViewSource x:Key="wares" Source="{Binding Source={StaticResource list}, Path=Items}" Filter="wareFilter"></CollectionViewSource>
        <CollectionViewSource x:Key="materials" Source="{Binding Source={StaticResource list}, Path=Items}" Filter="materialFilter"></CollectionViewSource>
        <CollectionViewSource x:Key="products" Source="{Binding Source={StaticResource list}, Path=Items}" Filter="productFilter"></CollectionViewSource>
    </Window.Resources>    
    <Grid>
        <Grid.RowDefinitions> 
            <RowDefinition Height="80"></RowDefinition>
            <RowDefinition Height="20"></RowDefinition>
            <RowDefinition Height="200"></RowDefinition>
            <RowDefinition Height="50"></RowDefinition>
            <RowDefinition Height="200"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>

        <StackPanel Grid.Row="0" Grid.ColumnSpan="2" Margin="5,5,5,5">
            <TextBox/>
            <Button Content="ok" Margin="0,5,0,0" HorizontalAlignment="Stretch" Height="30"  Width="150" Click="Button_Click"/>
        </StackPanel>

        <StackPanel Grid.RowSpan="2" Grid.Column="2">
            <ListBox Name="orders" IsEnabled="{Binding ElementName=check, Path=IsChecked}" Margin="85,5,85,5" Height="70" ItemsSource="{Binding Source={StaticResource list}}" DisplayMemberPath="SalesOrder"/>
            <CheckBox Name="check" HorizontalAlignment="Center" Content="Wybierz zamówienie" IsChecked="False"/>
        </StackPanel>

        <GroupBox Header="Wares" Grid.Row="2" Grid.Column="0">
            <ListBox Name="lbWares" ItemsSource="{Binding Source={StaticResource wares}}" >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <!--<StackPanel Orientation="Horizontal">-->
                        <TextBlock Text="{Binding Path=Name}"></TextBlock>
                            <!--<TextBlock Text="{Binding ZaE_TwrKod}" />
                            <TextBlock Text=", " />
                            <TextBlock Text="{Binding ZaE_Ilosc}" />
                            <TextBlock Text=", " />
                            <TextBlock Text="{Binding ZaE_TwrNazwa}" />-->
                        <!--</StackPanel>-->
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </GroupBox>

        <GroupBox Header="Materials" Grid.Row="2" Grid.Column="1">
            <ListBox Name="lbMaterials" ItemsSource="{Binding Source={StaticResource materials}}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <!--<StackPanel Orientation="Horizontal">-->
                            <TextBlock Text="{Binding Path=Name}"/>
                            <!--<TextBlock Text=", " />
                            <TextBlock Text="{Binding ZaE_Ilosc}" />
                            <TextBlock Text=", " />
                            <TextBlock Text="{Binding ZaE_TwrNazwa}" />-->
                        <!--</StackPanel>-->
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </GroupBox>

        <GroupBox Header="Products" Grid.Row="2" Grid.Column="2">
            <ListBox Name="lbProducts" ItemsSource="{Binding Source={StaticResource products}}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <!--<StackPanel Orientation="Horizontal">-->
                            <TextBlock Text="{Binding Path=Name}"/>
                            <!--<TextBlock Text=", " />
                            <TextBlock Text="{Binding ZaE_Ilosc}" />
                            <TextBlock Text=", " />
                            <TextBlock Text="{Binding ZaE_TwrNazwa}" />
                        </StackPanel>-->
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </GroupBox>
</Window>

DemoList对象绑定到CollectionViewList x:Key = list

这是我需要实施的商业逻辑:

  • 如果选中复选框,我需要将选定的ListBoxItem返回给 相应的容器[商品,产品,材料] - 这个逻辑是 工作
  • 如果复选框未标记,我需要返回所有项目
    [ObservableCollection]的ALL标题对应 容器[商品,产品,材料]

我被困在这里,有人可以建议我一个解决方案吗?

--- 2013-11-04 20:38

因为误会和我糟糕的英语而烦恼。

我上传了一些屏幕以便更清楚。

http://imgur.com/UowQrRP

正如您在屏幕上看到的,我需要实现复选框的行为。 取消选中时,每个DemoItem对象必须显示在3个容器中的一个容器中。 每个容器都绑定到CollectionViewSource。

0 个答案:

没有答案