【问题标题】:How to have a click-able button in my combo-box ItemTemplate?如何在我的组合框 ItemTemplate 中有一个可点击的按钮?
【发布时间】:2011-01-26 08:56:27
【问题描述】:

这是我目前所拥有的:

<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 中的按钮可以点击。

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

这就是现在的样子:

【问题讨论】:

    标签: wpf combobox datatemplate items


    【解决方案1】:

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

    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"   
          ...>
    

    【讨论】:

    • 布局部分完美运行。谢谢。现在是按钮。该按钮不可点击,我该如何处理呢?
    • 问题出在按钮的控制模板中。首先,删除按钮的模板。使图像成为按钮的内容部分。这不如您的解决方案好(将显示边框),但是您可以开发删除逻辑。完成删除逻辑后,谷歌搜索“ControlTemplate Button WPF”以查找有关如何为按钮创建控件模板的示例。另一种(便宜的)可能性是处理图像上的 PreviewMouseDown,但我不建议这样做。
    【解决方案2】:

    你的问题不够清楚。但我猜你想垂直对齐组合框中的 textimages。如果是这样,那么您只需要这样做:

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

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

    【讨论】:

    • @Nawaz:这没什么大的区别,100*和*一样,20是准等于“自动”的固定宽度。这里的重点是“Horizo​​ntalContentAlignment=Stretch”但是使用“Auto”确实比固定宽度更好......
    • @HCL:但只需* 就足够了。实际需要注意的是:Auto.
    • @Willem:如果它令人满意地解决了您的问题,那么接受它作为您问题的解决方案!
    • @Nawaz:不,绝对不,在他的示例中,您不会看到 auto 和 20 之间有任何区别,因为他已将控件的宽度明确定义为 20。这只是一个清洁问题.此外,在列定义中声明固定宽度是否也有意义,因为自动值可能会在复杂网格中造成严重的性能损失。但在这个例子中,这绝对不会发生。
    • @HCL:如果这样做能解决他的问题,我想听听他的意见。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-16
    相关资源
    最近更新 更多