【问题标题】:Change ListBox.ItemsSource Binding property on Button.Click?更改 Button.Click 上的 ListBox.ItemsSource 绑定属性?
【发布时间】:2011-08-10 17:37:08
【问题描述】:

快速提问...

我有一个ListBox,其ItemsSource 属性绑定到视图模型中的集合属性,如下所示:

<ListBox Name="CollectionsListBox" ItemsSource="{Binding Activity.Timesheets}" />

我在同一个视图中还有两个 Button 对象。问题是...我可以仅使用 XAML 将 CollectionsListBox ItemsSource BindingActivity.Timesheets 更改为 Activity.Attachments 吗?

失败了,从视图模型中使用命令对象?

编辑>>>

我在霍华德的部分回答中使用RadioButtons 而不是Buttons 找到了一个简单的解决方案:

<ListBox Name="CollectionsListBox">
    <ListBox.Style>
        <Style>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=TimesheetsButton,Path=IsChecked}" Value="True">
                    <Setter Property="ListBox.ItemsSource" Value="{Binding Activity.Timesheets}" />
                    <Setter Property="ListBox.ItemContainerStyle" Value="{StaticResource TimesheetStyle}" />
                </DataTrigger>
                <DataTrigger Binding="{Binding ElementName=AttachmentsButton,Path=IsChecked}" Value="True">
                    <Setter Property="ListBox.ItemsSource" Value="{Binding Activity.Attachments}" />
                    <Setter Property="ListBox.ItemContainerStyle" Value="{StaticResource AttachmentStyle}" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ListBox.Style>
</ListBox>

非常感谢您的帮助。

【问题讨论】:

    标签: wpf data-binding listbox itemssource


    【解决方案1】:

    我不确定 Button 是否可以做到这一点。但是单选按钮只能在 XAML 中满足你。

    假设我们有两个枚举:

    public enum E { A = 0, B = 1, C = 2 }
    public enum F { G = 0, H = 1, L = 2 }
    

    我在 XAML 中将它们定义为资源:

    <ObjectDataProvider ObjectType="{x:Type sys:Enum}" MethodName="GetValues" x:Key="EProvider">
        <ObjectDataProvider.MethodParameters>
            <x:TypeExtension Type="{x:Type local:E}" />
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
    
    <ObjectDataProvider ObjectType="{x:Type sys:Enum}" MethodName="GetValues" x:Key="FProvider">
        <ObjectDataProvider.MethodParameters>
            <x:TypeExtension Type="{x:Type local:F}" />
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
    

    那么我们开始吧:

    <ListBox x:Name="List1">
        <ListBox.Style>
            <Style>
                <Style.Triggers>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding ElementName=Rdb1,Path=IsChecked}" Value="True"/>
                        </MultiDataTrigger.Conditions>
                        <Setter Property="ListBox.ItemsSource" Value="{Binding Source={StaticResource EProvider}}" />
                    </MultiDataTrigger>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding ElementName=Rdb2,Path=IsChecked}" Value="True"/>
                        </MultiDataTrigger.Conditions>
                        <Setter Property="ListBox.ItemsSource" Value="{Binding Source={StaticResource FProvider}}" />
                    </MultiDataTrigger>
                </Style.Triggers>
            </Style>
        </ListBox.Style>
    </ListBox>
    <RadioButton x:Name="Rdb1" GroupName="Group1" />
    <RadioButton x:Name="Rdb2" GroupName="Group1" />
    

    【讨论】:

    • 当我绑定到视图模型时,我能够制作您示例的简化版本。非常感谢。
    • 不客气。我只是认为触发器有很多限制。也许我们可以讨论另一个问题。
    【解决方案2】:

    令我惊讶的是,以下似乎有效:

    <ListBox Name="myLB" ItemsSource="{Binding Data}"/>
    <Button Content="This is a Button">
        <Button.Triggers>
            <EventTrigger RoutedEvent="Button.Click">
                <BeginStoryboard>
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="myLB"
                                                       Storyboard.TargetProperty="ItemsSource">
                            <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{Binding Data2}"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Button.Triggers>
    </Button>
    

    编辑:这是否可行显然取决于 itemssource 的性质。动画在这方面有点混乱。使用恒定状态更好,例如正如 Radiobuttons 所建议的那样,从那时起可以使用 Setter。

    【讨论】:

    • 谢谢。最后我按照你的建议选择了RadioButtons。
    【解决方案3】:

    据我所知,这正是您所需要的 -

    这就是我的做法
    1)代码后面(有两个枚举)

     public enum Enum1{R = 0, O = 1,H = 2,I = 3,T = 4}
     public enum Enum2{A = 0,S = 1, I = 2,T = 3} 
     public partial class Window1 : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        private bool toggleItemSource;
        public bool ToggleItemSource
        {
            get
            {
                return this.toggleItemSource;
            }
            set
            {
                this.toggleItemSource = value;
                this.PropertyChanged(this, new PropertyChangedEventArgs("ToggleItemSource"));
            }
        }
    
        public Window1()
        {
            InitializeComponent();
            this.DataContext = this;
        }
    
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            this.ToggleItemSource = this.ToggleItemSource ? false : true;
        }
    }
    

    XAML

    <Window x:Class="Listbox_with_dynamic_data_source.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:local="clr-namespace:Listbox_with_dynamic_data_source"
    Title="Window1" Height="300" Width="300">
    

        <ObjectDataProvider ObjectType="{x:Type sys:Enum}"
                            MethodName="GetValues"
                            x:Key="Enum2Provider">
            <ObjectDataProvider.MethodParameters>
                <x:TypeExtension Type="{x:Type local:Enum2}" />
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </Window.Resources>
    <Grid>
        <!-- ListBox-->
        <ListBox x:Name="DynamicListBox"
                 Padding="10" HorizontalAlignment="Left" Width="52" Margin="20,21,0,115">
            <ListBox.Style>
                <Style TargetType="{x:Type ListBox}">
                    <Setter Property="ItemsSource"
                            Value="{Binding Source={StaticResource Enum1Provider}}"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=ToggleItemSource,
                                                       UpdateSourceTrigger=PropertyChanged
                                               }"
                                     Value="False">
                            <Setter Property="ItemsSource"
                                    Value="{Binding Source={StaticResource Enum2Provider}}"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ListBox.Style>
        </ListBox>
        <!-- Toggle Button -->
        <Button Height="29"
                Margin="94,45.44,59,0" 
                Name="button1" 
                VerticalAlignment="Top" 
                Click="button1_Click"
                Content="ToggleItemSource" />
    </Grid>
    

    点击切换项目来源按钮将切换项目来源

    【讨论】:

    • 感谢您的宝贵时间,但我找到了一个更简单的答案。
    • 我觉得有必要为您付出的努力投赞成票。至少它帮助了我。
    猜你喜欢
    • 1970-01-01
    • 2010-12-12
    • 2013-11-02
    • 2017-06-27
    • 2016-03-28
    • 1970-01-01
    • 2014-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多