Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change ListBox.ItemsSource Binding property on Button.Click?

Quick question...

I have a ListBox with its ItemsSource property bound to a collection property in a viewmodel like so:

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

I also have two Button objects in the same view. The question is... can I change the CollectionsListBox ItemsSource Binding from Activity.Timesheets to Activity.Attachments using just XAML?

Failing that, from the viewmodel using Command objects?

EDIT >>>

I found a simple solution by using RadioButtons instead of Buttons from part of Howard's answer:

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

Many thanks for the help.

like image 595
Sheridan Avatar asked Apr 22 '11 02:04

Sheridan


People also ask

What is the use of the itemssource property of listbox?

The ItemsSource property of ListBox is used to bind a collection of IEnumerable such as an ArrayList to the ListBox control. LeftListBox.

What are the properties of listbox in WPF?

Introduction to WPF ListBox Control. The XAML ListBox element represents a ListBox control. The Width and Height properties represent the width and the height of a ListBox. The Name property represents the name of the control, which is a unique identifier of a control. The Margin property tells the location of a ListBox on the parent control.

How to add listbox items dynamically using listbox event handler?

On button click event handler, we add the content of TextBox to the ListBox by calling ListBox.Items.Add method. Now, if you enter text in the TextBox and click Add Item button, it will add contents of the TextBox to the ListBox. Figure 3. Adding ListBox items dynamically

How to add and remove items from listbox using XAML?

The Add button adds the selected item to the right side ListBox and removes from the left side ListBox. The Remove button removes the selected item from the right side ListBox and adds back to the left side ListBox. The following XAML code generates two ListBox control and two Button controls.


2 Answers

I'm not sure if Button can do this. But radiobutton can satisfy you only in XAML.

Let's say we have two enum:

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

I defined them as resource in the 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>

Then here we go:

<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" />
like image 118
Howard Avatar answered Oct 02 '22 09:10

Howard


As much I understand this is what you need -
ListBox with Dynamic ItemSource

Here is how i did
1)Code Behind (with two enums)

 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>

Clicking on Toggle Item Source Button will toggle the Items Source

like image 28
Rohit Avatar answered Oct 02 '22 08:10

Rohit