简体   繁体   English

使用WPF中的不同内容按钮单击更新ListBox

[英]Updating a ListBox with different Content On Button Clicks in WPF

So I have a listbox and a tool bar in my WPF app. 所以我的WPF应用程序中有一个列表框和工具栏。 The tool bar just has regular controls, and the listbox has vertical expanders. 工具栏只有常规控件,列表框有垂直扩展器。

I need the listbox to have a different set of expanders depending on what button is clicked. 我需要列表框有一组不同的扩展器,具体取决于单击的按钮。 Right now it looks like such: 现在它看起来像这样:

<ListBox>
    <local:Select_Analysis_Panel/>
</ListBox>

Where local:Select_Analysis_Panel is seperate user control file containing the expanders. where local:Select_Analysis_Panel是包含扩展器的单独用户控制文件。 What is the best way to go about dynamically updating the ListBox control's content upon a button click? 单击按钮时动态更新ListBox控件内容的最佳方法是什么?

For the last couple hours I've been trying to use set DataTemplates for each expander set and bind the to the items control property with little avail with the code below. 在过去的几个小时里,我一直在尝试为每个扩展器集使用set DataTemplates,并使用下面的代码将item绑定到items控件属性。 I'm just trying to get basic framework laid out before setting up a MVVM interface. 我只是想在建立MVVM接口之前设置基本框架。 Later on I was going to replace the ItemsSource="Network_anal" with you know ItemsSource="{Binding WhatExpanderViewModelProperty}" or something like that. 后来我打算用你知道的ItemsSource="{Binding WhatExpanderViewModelProperty}"或类似的东西替换ItemsSource="Network_anal"

 <ListBox Width="250"  Margin="5,0,0,0">
            <ListBox.Resources>

                <DataTemplate DataType="Select_Analysis_Panel">
                    <local:Select_Analysis_Panel/>
                </DataTemplate>

                <DataTemplate x:Key="Network_anal" DataType="NetworkAnalysis">
                    <local:NetworkAnalysis/>
                </DataTemplate>.Resources>

            <ListBox.Template>                    
                <ControlTemplate>
                    <Border Background="Red"/>
                </ControlTemplate>                    
            </ListBox.Template>

            <ItemsControl ItemsSource="Network_anal"/>
</ListBox>

Am I taking the right approach to this at all? 我是否采取了正确的方法?

Here's what I'm trying to do. 这就是我想要做的。 Below when the "File" button is clicked the side bar displays these 2 expanders: 在单击“文件”按钮时,侧栏显示以下2个扩展器:

在此输入图像描述 And when "Network Design" button these expanders are dipslayed: 当“网络设计”按钮时,这些扩展器会被丢弃:

在此输入图像描述

Option 1: 选项1:

Subclassing the sections: 对章节进行子类化:

each of these sections could be subclassed from a base section class and a specific DataTemplate could be used for each: 这些部分中的每一部分都可以从基础部分类中进行子类化,并且可以为每个部分使用特定的DataTemplate

<Window x:Class="MiscSamples.MultiToolbar"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:MiscSamples"
        Title="MultiToolbar" Height="300" Width="300">
    <Window.Resources>
        <BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
    </Window.Resources>
    <DockPanel>
        <ListBox ItemsSource="{Binding Sections}"
                 SelectedItem="{Binding SelectedSection}"
                 DisplayMemberPath="Name"
                 DockPanel.Dock="Top">
            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Setter Property="IsEnabled" Value="{Binding IsEnabled}"/>
                    <Setter Property="Visibility" Value="{Binding IsVisible, Converter={StaticResource BoolToVisibilityConverter}}"/>
                    <Setter Property="MinWidth" Value="80"/>
                    <Setter Property="MinHeight" Value="40"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="ListBoxItem">
                                <Border BorderBrush="Black" BorderThickness="1">
                                    <ToggleButton IsChecked="{Binding IsSelected, Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}">
                                        <ContentPresenter ContentSource="Content"/>
                                    </ToggleButton>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListBox.ItemContainerStyle>
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
        </ListBox>

        <ScrollViewer Width="300" DockPanel.Dock="Left">
            <ContentPresenter Content="{Binding SelectedSection}">
                <ContentPresenter.Resources>
                    <DataTemplate DataType="{x:Type local:FileSection}">
                        <TextBlock Text="User Control For File Section"/>
                    </DataTemplate>
                    <DataTemplate DataType="{x:Type local:NetworkDesignSection}">
                        <TextBlock Text="User Control For Network Design"/>
                    </DataTemplate>
                    <DataTemplate DataType="{x:Type local:SelectAnalysisSection}">
                        <TextBlock Text="User Control For Select Analysis"/>
                    </DataTemplate>
                </ContentPresenter.Resources>
            </ContentPresenter>
        </ScrollViewer>

        <Grid Background="Gray">
            <TextBlock Text="Design Surface" TextAlignment="Center" VerticalAlignment="Center" FontWeight="Bold"/>
        </Grid>
    </DockPanel>
</Window>

Code Behind: 代码背后:

public partial class MultiToolbar : Window
    {
        public MultiToolbar()
        {
            InitializeComponent();

            var vm = new MainViewModel();
            vm.Sections.Add(new FileSection() {Name = "File"});
            vm.Sections.Add(new NetworkDesignSection() { Name = "Network Design" });
            vm.Sections.Add(new SelectAnalysisSection() { Name = "Select Analysis" });

            DataContext = vm;
        }
    }

Main ViewModel: 主视图模型:

public class MainViewModel: PropertyChangedBase
    {
        private ObservableCollection<Section> _sections;
        public ObservableCollection<Section> Sections
        {
            get { return _sections ?? (_sections = new ObservableCollection<Section>()); }
        }

        private Section _selectedSection;
        public Section SelectedSection
        {
            get { return _selectedSection; }
            set
            {
                _selectedSection = value;
                OnPropertyChanged("SelectedSection");
            }
        }
    }

Sections: 部分:

public abstract class Section:PropertyChangedBase
    {
        public string Name { get; set; }

        private bool _isEnabled = true;
        public bool IsEnabled
        {
            get { return _isEnabled; }
            set
            {
                _isEnabled = value;
                OnPropertyChanged("IsEnabled");
            }
        }

        private bool _isVisible = true;
        public bool IsVisible
        {
            get { return _isVisible; }
            set
            {
                _isVisible = value;
                OnPropertyChanged("IsVisible");
            }
        }

        //Optionally
        //public string ImageSource {get;set;}
        //ImageSource = "/Resources/MySection.png";
    }

    public class FileSection: Section
    {
        ///... Custom logic specific to this Section
    }

    public class NetworkDesignSection:Section
    {
        ///... Custom logic specific to this Section
    }

    public class SelectAnalysisSection: Section
    {
        ///... Custom logic specific to File Section
    }

    //...etc etc etc

Result: 结果:

在此输入图像描述

Notice that I'm using ToggleButton s bound to the ListBoxItem.IsSelected property to simulate a TabControl -like behavior. 请注意,我正在使用绑定到ListBoxItem.IsSelected属性的ToggleButton来模拟类似TabControl的行为。

您可以设置整个表单的DataContext并绑定listboxItemsSource ,或者直接将listbox ItemsSource设置为某个集合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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