在Button.Click上更改ListBox.ItemsSource绑定属性?

时间:2011-04-22 02:11:45

标签: wpf data-binding listbox itemssource

快速提问......

我有一个ListBox,其ItemsSource属性绑定到viewmodel中的collection属性,如下所示:

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

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

使用Command对象从viewmodel失败吗?

编辑&gt;&gt;&gt;

我找到了一个简单的解决方案,使用RadioButton代替Button来自霍华德答案的一部分:

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

非常感谢你的帮助。

3 个答案:

答案 0 :(得分:8)

我不确定Button是否可以这样做。但是radiobutton只能在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" />

答案 1 :(得分:3)

据我所知,这就是你所需要的 -
  ListBox with Dynamic ItemSource

这是我怎么做的 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>

单击切换项目源按钮将切换项目源

答案 2 :(得分: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。