简体   繁体   中英

Binding function to textblock in listbox c# WPF

I'm learning MVVM so it can be newbie questions.

I need to bind the function:

private void doubleClick(object sender, MouseButtonEventArgs e)

in the textblock :

<Grid>
    <ListBox Name="mediaList" Grid.Row="1"
             ItemsSource="{Binding Medias}"
             IsSynchronizedWithCurrentItem="True">
        <ListBox.ItemTemplate>
            <DataTemplate DataType="{x:Type Models:Media}">
                <StackPanel Orientation="Horizontal">
                    <Image Source="icon-play-128.png" Margin="0,0,5,0" />
                    <TextBlock Text="{Binding Name}" Margin="0,0,5,0">
                        <TextBlock.InputBindings>
                            <MouseBinding Command="{Binding DoubleClick}" Gesture="LeftDoubleClick" />
                        </TextBlock.InputBindings>
                    </TextBlock>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

but the first problem is that DoubleClick is in my view-model and not in my class media (in Model).

The other problem is that I need to receive two parameters how can I do this?

If you have better ways to do please explain to me.

Thanks in advance.

  1. To link Command and its Execute method, you can use delegates. MSDN has very good example demonstrating this. See the example there.

  2. Secondly, you are actually trying to handle MouseDoubleClick event. But MouseDoubleClick event is exposed by Control class. And TextBlock is not a Control. So, best would be to wrap your TextBlock inside a ContentControl.

     <ContentControl MouseDoubleClick="ContentControl_MouseDoubleClick"> <TextBlock ... /> </ContentControl> 

Then from your event handler, invoke your command programmatically.

  1. If you want to keep everything de-coupled, and handling that event is very important, then write a behavior. In that Behavior, you can attach your MouseDoubleClick event handler, and do whatever you like. You can introduce your own properties in a Behavior suitable for your needs.

     using System.Windows.Interactivity; public class MyBehavior : Behavior<ContentControl> { public MyBehavior() {} protected override void OnAttached() { AssociatedObject.MouseDoubleClick += AssociatedObject_MouseDoubleClick; base.OnAttached(); } protected override void OnDetaching() { AssociatedObject.MouseDoubleClick -= AssociatedObject_MouseDoubleClick; } void AssociatedObject_MouseDoubleClick(object sender, MouseButtonEventArgs e) { // do something } } 

XAML usage :

<ContentControl ...
      xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" >
   <TextBlock .../>
   <i:Interaction.Behaviors>
       <local:MyBehavior />       
   </i:Interaction.Behaviors>
</ContentControl>    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM