获取列表框行对象从一个按钮,是对的DataTemplate [英] Get the ListBox row object from a button that is on the DataTemplate

查看:255
本文介绍了获取列表框行对象从一个按钮,是对的DataTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表框的DataTemplate 。该模板有一个按钮就可以了。当按钮点击我想要做一些逻辑与的每一行(在这种情况下,所谓的对象 WorkItemTypeMappings )。

的OnClick 我怎么能去从按钮对象发件人)来表示是排的按钮上?对象

这里是我的XAML 列表框

 <列表框的ItemsSource ={绑定源= {的StaticResource WorkItemTypeMappingsCollectionView}}Horizo​​ntalContentAlignment =拉伸ScrollViewer.Horizo​​ntalScrollBarVisibility =已禁用NAME =lstWITypes>
    < ListBox.GroupStyle>
        < X:静态成员=GroupStyle.Default/>
    < /ListBox.GroupStyle>
    < ListBox.ItemTemplate>
        <的DataTemplate>
            <网格的Horizo​​ntalAlignment =拉伸>
                < Grid.ColumnDefinitions>
                    < ColumnDefinition宽度=2 */>
                    < ColumnDefinition宽度=2 */>
                    < ColumnDefinition WIDTH =50/>
                < /Grid.ColumnDefinitions>
                &所述; TextBlock的Grid.Column =0的文本={结合SourceType中,转换器= {的StaticResource WorkItemTypeToStringConverter}}/>
                <组合框Grid.Column =1的SelectedItem ={结合DESTTYPE}的ItemsSource ={结合WorkItemTypesForCurrentDestProject,源= {X:静态LOC:MainMediator.Instance},诊断:presentationTraceSources.TraceLevel =高}的DisplayMemberPath =名称/>

                <! - 这是一个按钮 - >
                &所述;按钮Grid.Column =2内容={结合PercentMapped}
                        点击=ManualMappingClick/>
            < /网格>
        < / DataTemplate中>
    < /ListBox.ItemTemplate>
< /列表框>
 

解决方案

你可以做什么作为一个替代方案是使用,而不是事件的命令。如果你的按钮绑定到一个命令,并与它一起传递命令参数,你应该能够得到的是关系到该按钮的项目。例如:code:

 <! - 这是一个按钮 - >
 <按钮
     Grid.Column =2
     内容={结合PercentMapped}
     命令=SolutionNamespace:CommandClass.ButtonClickedCommand
     CommandParameter ={结合}/>
 

我不知道你是如何熟悉与WPF指挥,但你会发现,CommandParameter结合上下文没有路径名,也就是说它绑定到你需要的WorkItemTypeMappings。

命令示例code:

 公共静态SimpleCommand ButtonClickedCommand {获得;组; }

静态CommandClass()
{
    ButtonClickedCommand =新SimpleCommand
                           {
                               ExecuteDelegate =
                               X => ButtonClicked(x作为WorkItemTypeMappings)
                           };
}


公共静态ButtonClicked(WorkItemTypeMappings映射)
{
    如果(映射!= NULL)的MessageBox.show(mapping.PercentMapped)
}
 

您会发现,ButtonClickedCommand是静态的,这是必需的,因为按钮无法从当前绑定上下文访问命令。

SimpleCommand 是ICommand的只是一个简单的实现,可以谷歌这一个,如果你不知道。我希望这不是矫枉过正您的问题,但你不能去错了WPF命令。

祝你好运。

I have a ListBox with a DataTemplate. The template has a Button on it. When the Button is clicked I want to do some logic with the object that is each row (in this case an object called WorkItemTypeMappings).

In theOnClick how can I go from the Button (object sender) to the object that is row that the button is on?

Here is the XAML of my ListBox:

<ListBox ItemsSource="{Binding Source={StaticResource WorkItemTypeMappingsCollectionView}}" HorizontalContentAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Name="lstWITypes">
    <ListBox.GroupStyle>
        <x:Static Member="GroupStyle.Default"/>
    </ListBox.GroupStyle>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid HorizontalAlignment="Stretch">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="2*"/>
                    <ColumnDefinition Width="2*"/>
                    <ColumnDefinition Width="50"/>
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" Text="{Binding SourceType, Converter={StaticResource WorkItemTypeToStringConverter}}"/>
                <ComboBox Grid.Column="1" SelectedItem="{Binding DestType}" ItemsSource="{Binding WorkItemTypesForCurrentDestProject, Source={x:Static loc:MainMediator.Instance}, diagnostics:PresentationTraceSources.TraceLevel=High}" DisplayMemberPath="Name" />

                <!-- This is the button-->
                <Button Grid.Column="2" Content="{Binding PercentMapped}" 
                        Click="ManualMappingClick"/>
            </Grid>
        </DataTemplate>                                        
    </ListBox.ItemTemplate>
</ListBox>

解决方案

What you can do as an alternative is to use Command instead of Event. If you bind your button to a command and pass along with it a command parameter, you should be able to get the item that is related to that button. Example code:

<!-- This is the button-->
 <Button
     Grid.Column="2"
     Content="{Binding PercentMapped}" 
     Command="SolutionNamespace:CommandClass.ButtonClickedCommand"
     CommandParameter="{Binding}" />

I am not sure how familiar you are with WPF commanding but you will notice that the CommandParameter binds to the context without a path name, that is to say it binds to the WorkItemTypeMappings that you need.

Command Example Code:

public static SimpleCommand ButtonClickedCommand { get; set; }

static CommandClass()
{
    ButtonClickedCommand = new SimpleCommand
                           {
                               ExecuteDelegate =
                               x=> ButtonClicked(x as WorkItemTypeMappings)
                           };
}


public static ButtonClicked(WorkItemTypeMappings mappings)
{
    if(mappings != null) MessageBox.Show(mapping.PercentMapped)
}

You will notice that the ButtonClickedCommand is static, this is required because the button cannot access the command from its current binding context.

SimpleCommand is just a simplified implementation of the ICommand, can Google this one if you're not sure. I hope this is not an overkill to your problem, but you cannot go wrong with WPF Commands.

Good luck.

这篇关于获取列表框行对象从一个按钮,是对的DataTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆