2011-04-22 182 views
9

快速的问题...在Button.Click上更改ListBox.ItemsSource绑定属性?

我有它的ItemsSource属性ListBox绑定到一个集合属性在视图模型像这样:

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

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

失败,从使用Command对象的视图模型?

编辑>>>

我发现了一个简单的解决方案通过使用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> 

的帮助非常感谢。

回答

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" /> 
+0

因为我绑定到视图模型,我就能够让你的实例的简化版本。非常感谢。 – Sheridan 2011-04-22 19:52:00

+0

不客气。我只是觉得触发器有很多限制。也许我们可以有另一个问题在谈论这个问题。 – Howard 2011-04-23 02:24:33

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的性质。动画在这方面有点混乱。使用恒定状态更好,例如正如Radibuttons所建议的那样,Setters可以被使用。

+0

谢谢。就像你所说的那样,我最终用了RadioButton。 – Sheridan 2011-04-22 19:53:10

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> 

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

+0

感谢您的时间,但我找到了一个更简单的答案。 – Sheridan 2011-04-25 11:04:01

+0

好吧! NP:P – Rohit 2011-04-25 11:05:16

+0

我觉得有必要给你一个赞成花费的努力。至少它帮助了我。 – Mizipzor 2011-11-24 15:35:06