如何在组合框 ItemTemplate 中添加可点击的按钮?

发布于 2024-10-14 18:31:51 字数 2208 浏览 2 评论 0原文

这就是我到目前为止所拥有的:

<dxe:ComboBoxEdit Name="cboUserCustomReports"
                      Width="300" Height="Auto"
                      Margin="0,5,0,5"
                      ItemsSource="{Binding Path=UserReportProfileList,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"
                      EditValue="{Binding Path=UserReportProfileID,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                      ValueMember="UserReportProfileID"
                      DisplayMember="ReportName"
                      PopupClosed="cboUserCustomReports_PopupClosed">
            <dxe:ComboBoxEdit.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="100*"/>
                            <ColumnDefinition Width="20"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.Column="0"
                                   Text="{Binding ReportName, Mode=Default}" 
                                   VerticalAlignment="Stretch" HorizontalAlignment="Left"/>
                        <Button Name="btnDelete" 
                                Grid.Column="1"
                                Width="20" Height="20"
                                VerticalAlignment="Center" HorizontalAlignment="Right"
                                Click="btnDelete_Click">
                            <Button.Template>
                                <ControlTemplate>
                                    <Image Source="/RMSCommon;component/Resources/Delete.ico"></Image>
                                </ControlTemplate>
                            </Button.Template>
                        </Button>
                    </Grid>
                </DataTemplate>
            </dxe:ComboBoxEdit.ItemTemplate>
        </dxe:ComboBoxEdit>

首先,我希望这两列是独立的。用户必须能够选择或删除该项目。

其次,我想让 ItemTemplate 中的按钮可单击。

我需要添加什么才能获得这种行为?

这是目前的样子:

在此处输入图像描述

This is what i have so far:

<dxe:ComboBoxEdit Name="cboUserCustomReports"
                      Width="300" Height="Auto"
                      Margin="0,5,0,5"
                      ItemsSource="{Binding Path=UserReportProfileList,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"
                      EditValue="{Binding Path=UserReportProfileID,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                      ValueMember="UserReportProfileID"
                      DisplayMember="ReportName"
                      PopupClosed="cboUserCustomReports_PopupClosed">
            <dxe:ComboBoxEdit.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="100*"/>
                            <ColumnDefinition Width="20"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.Column="0"
                                   Text="{Binding ReportName, Mode=Default}" 
                                   VerticalAlignment="Stretch" HorizontalAlignment="Left"/>
                        <Button Name="btnDelete" 
                                Grid.Column="1"
                                Width="20" Height="20"
                                VerticalAlignment="Center" HorizontalAlignment="Right"
                                Click="btnDelete_Click">
                            <Button.Template>
                                <ControlTemplate>
                                    <Image Source="/RMSCommon;component/Resources/Delete.ico"></Image>
                                </ControlTemplate>
                            </Button.Template>
                        </Button>
                    </Grid>
                </DataTemplate>
            </dxe:ComboBoxEdit.ItemTemplate>
        </dxe:ComboBoxEdit>

First, i want the two columns to be stand alone. The user must be able to select or delete the item.

Second, i would like to make my button in the ItemTemplate to be click-able.

What do i need to add to get this behavior?

This is what it looks like at the moment:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨巷深深 2024-10-21 18:31:51

点击
我假设您的按钮是可点击的,并且您想知道如何处理点击事件。对吗?
对于点击处理程序,添加以下代码:

private void btnDelete_Click(object sender, RoutedEventArgs e) {
    FrameworkElement fe = sender as FrameworkElement;
    if(null == fe){
        return;
    }
    UserReportProfile userReportProfile = fe.DataContext as UserReportProfile;
    if (null == userReportProfile) {
        return;
    }
    // Do here your deletion-operation

}

我假设您的项目类名为 UserReportProfile。否则,相应地更改声明的类型。

布局
对于对齐方式,将以下声明添加到 ComboBox:

HorizontalContentAlignment="Stretch" 

这将为您的 DataTemplate-Grid 提供完整宽度,您可以根据需要布局项目。

<dxe:ComboBoxEdit Name="cboUserCustomReports"                         
      HorizontalContentAlignment="Stretch"            
      Width="300" Height="Auto"                         
      Margin="0,5,0,5"   
      ...>

Click
I assume, that your button is clickable, and you want to know how to process the click event. Right?
For the click-handler, add the following code:

private void btnDelete_Click(object sender, RoutedEventArgs e) {
    FrameworkElement fe = sender as FrameworkElement;
    if(null == fe){
        return;
    }
    UserReportProfile userReportProfile = fe.DataContext as UserReportProfile;
    if (null == userReportProfile) {
        return;
    }
    // Do here your deletion-operation

}

I assumed that your item-class is named UserReportProfile. Otherwise, change the declared type accordingly.

Layout
For the alignment, add the following declaration to your ComboBox:

HorizontalContentAlignment="Stretch" 

This gives your DataTemplate-Grid the full width and you can layout then your items as you desire.

<dxe:ComboBoxEdit Name="cboUserCustomReports"                         
      HorizontalContentAlignment="Stretch"            
      Width="300" Height="Auto"                         
      Margin="0,5,0,5"   
      ...>
爱殇璃 2024-10-21 18:31:51

你的问题不够明确。但我猜您想垂直对齐组合框中的文本图像。如果是这样,那么您所需要做的就是:

<Grid.ColumnDefinitions>
   <ColumnDefinition Width="*"/>
   <ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>

而且我认为您的项目已经可以点击了!

Your question is not clear enough. But I guess you want to vertically align the text and images in your combobox. If so, then all you need to do this:

<Grid.ColumnDefinitions>
   <ColumnDefinition Width="*"/>
   <ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>

And I think your items are already clickable!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文