【问题标题】:Updating a ListBox with different Content On Button Clicks in WPF在 WPF 中更新具有不同内容的列表框按钮单击
【发布时间】:2013-04-09 17:13:57
【问题描述】:

所以我的 WPF 应用程序中有一个列表框和一个工具栏。工具栏只有常规控件,列表框有垂直扩展器。

我需要列表框有一组不同的扩展器,具体取决于单击的按钮。现在看起来是这样的:

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

其中local:Select_Analysis_Panel 是包含扩展器的单独用户控制文件。单击按钮时动态更新 ListBox 控件内容的最佳方法是什么?

在过去的几个小时里,我一直在尝试为每个扩展器集使用 set DataTemplates 并将其绑定到项目控件属性,但使用下面的代码几乎没有用处。我只是想在设置 MVVM 接口之前布置基本框架。后来我打算用你知道的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>

我是否采取了正确的方法来解决这个问题?

这就是我想要做的。当点击“文件”按钮时,下面的侧边栏会显示这两个扩展器:

当“网络设计”按钮时,这些扩展器会显示:

【问题讨论】:

  • 再发一张你需要的截图……
  • 顺便问一下,“节点编辑器”的一切进展如何?
  • 非常非常非常非常感谢你,仍然使用你的模板,太棒了。立即获取屏幕截图
  • 哇....我非常喜欢
  • 是的,它一直伴随着兄弟!您对使用 MVVM 的建议非常有帮助

标签: c# .net wpf data-binding


【解决方案1】:

选项 1:

子类化部分:

这些部分中的每一个都可以从基本部分类中继承,并且每个部分都可以使用特定的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>

代码背后:

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

主视图模型:

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

部分:

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

结果:

请注意,我使用绑定到ListBoxItem.IsSelected 属性的ToggleButtons 来模拟类似TabControl 的行为。

【讨论】:

  • 哈哈你接受了答案,没有给我时间添加选项2!这是关于在部分中放置一个IEnumerable&lt;SectionOption&gt; 并使用它来渲染ExpandersCheckBoxes 以及其他内容。
  • 非常感谢先生。我相信这会很好......它看起来很棒!
  • 嘿,我可能有点过激了...如果你还有代码,你能告诉我另一种方式吗?如果它不方便,请不要打扰......我的选项 1 工作正常,为另一个面板实现类似的东西会有点困难(右侧将有另一个面板有一组不同的左侧每个复选框的按钮!)
  • @SteelNation 我认为这很复杂,值得单独提出一个问题,你不觉得吗?发布一个新问题,让我知道! =)
  • 好的,在这里重新发布:stackoverflow.com/questions/15935011/…
【解决方案2】:

可以设置整个表单的DataContext,绑定listboxItemsSource,或者直接设置listboxItemsSource到某个集合。

【讨论】:

    猜你喜欢
    • 2012-10-13
    • 2021-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-15
    相关资源
    最近更新 更多