简体   繁体   English

WPF ItemsControl 中 DataTemplate 内的访问按钮

[英]Access Button inside DataTemplate in WPF ItemsControl

I want to access the Button inside MouseDown event, I have the following:我想访问 MouseDown 事件中的 Button,我有以下内容:

XAML: XAML:

<ItemsControlx:Name="icName" MouseDown="icItems_MouseDown" >
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Button x:Name="btnName" Tag="{Binding ItemName}"</Button>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
 </ItemsControl>

C#: C#:

private void icName_MouseDown(object sender, MouseButtonEventArgs e)
    {
       ???
    }

How can I access to the button from ItemsControl MouseDown event如何从 ItemsControl MouseDown 事件访问按钮

ex: MessageBox.Show(ItemName);例如:MessageBox.Show(ItemName);

Thanks谢谢

Please refer to the following sample code.请参考以下示例代码。

private void icName_MouseDown(object sender, MouseButtonEventArgs e)
{
    ContentPresenter cp = e.OriginalSource as ContentPresenter;
    if (cp != null && VisualTreeHelper.GetChildrenCount(cp) > 0)
    {
        Button button = VisualTreeHelper.GetChild(cp, 0) as Button;
        //do whatever you want with the Button here...
        if (button != null && button.Tag != null)
            MessageBox.Show(button.Tag.ToString());

    }
}

<ItemsControl x:Name="icName" MouseDown="icName_MouseDown" >
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button x:Name="btnName" Content="Button" IsEnabled="False" Tag="{Binding ItemName}"></Button>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

if you don't need access to the button, but only need the item name from the bound item, then personally I would add the handler inside the datatemplate, I'm using a grid below.如果您不需要访问按钮,而只需要绑定项目中的项目名称,那么我个人会在数据模板中添加处理程序,我正在使用下面的网格。

<ItemsControl x:Name="icName" >
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid MouseDown="icItems_MouseDown" />
                    <Button x:Name="btnName" Tag="{Binding ItemName}"</Button>
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
 </ItemsControl>

Then you can access your datacontext from within the handler.然后您可以从处理程序中访问您的数据上下文。

    private void icName_MouseDown(object sender, MouseButtonEventArgs e)
    {
       var item = (YourItemType)((FrameworkElement)sender).DataContext;
       var itemName = item.ItemName;
    }

You also don't need the tag at all你也根本不需要标签

You can't handle a click on a group of buttons and know which one is clicked.您无法处理对一组按钮的单击并知道单击了哪个按钮。 You should handle the click for each button with the same handler like this:您应该使用相同的处理程序处理每个按钮的点击,如下所示:

<Button x:Name="btnName" Tag="{Binding ItemName}"  MouseDown="icItem_MouseDown"></Button>

Then the sender is the calling button:然后sender是呼叫按钮:

private void icItem_MouseDown(object sender, MouseButtonEventArgs e)
{
   var btn = sender as Button;
   if(btn!=null)
       MessageBox.Show(btn.Tag);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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