9

快速提问...

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

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

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

做不到这一点,从使用命令对象的视图模型?

编辑>>>

我从霍华德的部分答案中使用RadioButtons 而不是s 找到了一个简单的解决方案: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>

非常感谢您的帮助。

4

3 回答 3

8

我不确定 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" />
于 2011-04-22T03:07:54.160 回答
3

据我了解,这就是您所需要的-
具有动态 ItemSource 的 ListBox

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

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

于 2011-04-22T08:48:29.280 回答
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。

于 2011-04-22T03:30:54.143 回答