Reputation: 1925
I am creating a WPF application. I want a page to be displayed on each button click.
<ItemsControl ItemsSource="{Binding Path=DashBoardApps}" VerticalAlignment="Bottom" HorizontalAlignment="Center" Name="Abc">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Controls:FishEyeControl />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button MouseDown="Button_MouseDown">
<Image Source="{Binding Path=ApplicationImage}" Width="32" Height="32"/>
</Button>
<TextBlock x:Name="txtAppName" Text="{Binding Path=ApplicationName}" TextAlignment="Center" Visibility="Hidden" FontSize="7px" Foreground="#eff7ff" VerticalAlignment="Center"/>
</StackPanel>
<DataTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="txtAppName" Property="Visibility" Value="Visible" />
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
There are 6 buttons in total.On the button click I want a page to be displayed.(different pages for different buttons). In the code behind I've done something like this
private void Button_MouseDown(object sender, MouseButtonEventArgs e)
{
var s = sender as ItemsControl;
if (s == Abc.Items.GetItemAt(1))//if it is the first button. Not sure if it is right
{
//Code to display page
}
}
I want to know which button generated the event. As in, if the first button was clicked then page1 has to be displayed. So I tried using GetItemAt()
function to see if the first button was clicked(to display the first page). However it doesn't work and I am not sure if this approach is correct.
P.S Simply stated I want to know which button generated the event-Can this be done using index. If so, how? Kindly help
Upvotes: 0
Views: 1735
Reputation: 128061
The actual Button is passed to the event handler in the sender argument:
Button button = sender as Button;
There is however a problem. Since Button handles the MouseDown event, you won't get it. You would have to either add a handler for the PreviewMouseDown event, or better for the Button's Click event.
And just as a note: The ItemCollection.GetItemAt method uses a zero-based index.
EDIT: Here is how to bind the Button's Tag
property to the ItemsControl's AlternationIndex
property and how to utilize that index in the Button's Click handler:
<ItemsControl ... AlternationCount="2147483647">
...
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button Click="Button_Click"
Tag="{Binding RelativeSource={RelativeSource Mode=TemplatedParent},
Path=(ItemsControl.AlternationIndex)}">
...
</StackPanel>
...
</DataTemplate>
</ItemsControl.ItemTemplate>
With the XAML namespace xmlns:sys="clr-namespace:System;assembly=mscorlib"
you could alternatively write
<ItemsControl AlternationCount="{x:Static sys:Int32.MaxValue}">
The Click handler:
private void Button_Click(object sender, RoutedEventArgs e)
{
Button button = sender as Button;
int index = (int)button.Tag;
// get page by zero-based index
...
}
Upvotes: 3