Zwan
Zwan

Reputation: 642

Click event on an item template

I have this wpf code for my listbox item:

 <ListBox x:Name="icTodoList" ItemsSource="{Binding ListControleMachine}" Grid.Column="3">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Border BorderBrush="{Binding borderColor}" BorderThickness="2" Margin="0,0,0,1">
                <Grid Margin="1">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="30"/>
                        <ColumnDefinition Width="20"/>
                        <ColumnDefinition Width="100"/>
                        <ColumnDefinition Width="120"/>
                        <ColumnDefinition Width="130"/>
                        <ColumnDefinition Width="120"/>
                        <ColumnDefinition Width="120"/>
                        <ColumnDefinition Width="120"/>
                        <ColumnDefinition Width="120"/>
                    </Grid.ColumnDefinitions>
                    <CheckBox x:Name="check_action" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" IsChecked="{Binding ActionCheked,Mode=TwoWay}"/>
                    <Ellipse x:Name="E_Ping" HorizontalAlignment="Left" Fill="{Binding PingDotColor}" Height="10" Width="10" Margin="2,4,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.2,-1.182" Grid.Column="1"/>
                    <TextBlock Text="{Binding titlePing}" Grid.Column="2" MouseDown="TextBlock_MouseDown" Tag="{Binding ComputerName}"/>
                    <TextBlock Text="{Binding titlePing}" Grid.Column="2" MouseDown="TextBlock_MouseDown" Tag="{Binding ComputerName}"/>
                    <TextBlock Text="{Binding titleDcai}" Grid.Column="3" MouseDown="TextBlock_MouseDown" Tag="{Binding ComputerName}"/>
                    <TextBlock Text="{Binding titleAd}" Grid.Column="4" MouseDown="TextBlock_MouseDown" Tag="{Binding ComputerName}"/>
                    <TextBlock Text="{Binding FPACtitle}" Grid.Column="5" MouseDown="TextBlock_MouseDown" Tag="{Binding ComputerName}"/>
                    <TextBlock Text="{Binding titleMcAfee}" Grid.Column="6" MouseDown="TextBlock_MouseDown" Tag="{Binding ComputerName}"/>
                    <TextBlock Text="{Binding gkUser}" Grid.Column="7" MouseDown="TextBlock_MouseDown" Tag="{Binding ComputerName}"/>
                    <TextBlock Text="{Binding titleservicestat}" Grid.Column="8" MouseDown="TextBlock_MouseDown" Tag="{Binding ComputerName}"/>
                 </Grid>
            </Border>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

As you can see I have a "TextBlock_MouseDown" on each textbox

Then the c# code.

private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
{
     TextBlock t = sender as TextBlock;
     DetailedView.DataContext = ListControleMachine.Where(x => x.ComputerName == t.Tag.ToString());
     DetailedView.Visibility = System.Windows.Visibility.Visible;  
}

Ok, so it does what I need basically it pop a view with data rebinded to it.

but I'm pretty sure its not a clean way to do it.

first I don't wont event on each textblock but on the listboxitem it self (but I don't know how to pass computername argument this way).

second and may bee my only problem I think I don't have to search my collection and rebind my new view with this:

DetailedView.DataContext =  ListControleMachine.Where(x => x.ComputerName == t.Tag.ToString());

for be clear how to click an item template and pop a view that will contain actual listbox item binded data?

Upvotes: 2

Views: 925

Answers (1)

ASh
ASh

Reputation: 35733

try set DataContext via binding to SelectedItem of ListBox

<DetailedView DataContext="{Binding Path=SelectedItem, ElementName=icTodoList, Mode=OneWay}"/>

or equivalent code

var binding = new Binding
{
    Path = new PropertyPath("SelectedItem"),
    ElementName = "icTodoList", 
    Mode = BindingMode.OneWay
};
BindingOperations.SetBinding(DetailedView, Control.DataContextProperty, binding);


another approach

ListBox has SelectionChanged event

<ListBox Name="icTodoList" 
            SelectionChanged="IcTodoList_OnSelectionChanged">

</ListBox>

event handler

private void IcTodoList_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    DetailedView.DataContext = icTodoList.SelectedItem;
    DetailedView.Visibility = System.Windows.Visibility.Visible;  
}

Tag values and TextBlock_MouseDown handler will be not necessary

Upvotes: 1

Related Questions