【问题标题】:WPF ListBox selection issueWPF ListBox 选择问题
【发布时间】:2012-07-02 19:08:42
【问题描述】:

我正在开发带有大量自定义控件的应用程序。 我有一个带有 ListBox 的自定义控件,其中包含其他自定义控件的集合。

这里是这 2 个自定义控件(容器和元素)的控件模板

容器(继承控件):

<ControlTemplate TargetType="{x:Type local:Container}">
<Border Background="LightBlue"
        BorderBrush="RoyalBlue"
        BorderThickness="2" CornerRadius="5">                                               
    <StackPanel>                                                    
        <TextBlock TextAlignment="Center" Text="{TemplateBinding Name}" />
        <ListBox ItemsSource="{TemplateBinding Items}">
        </ListBox>
    </StackPanel>
</Border>
</ControlTemplate>

元素(继承 ListBoxItem):

<ControlTemplate TargetType="{x:Type local:Element}">
    <Border IsHitTestVisible="True" >                        
        <TextBlock Text="{TemplateBinding ElementName}" />
    </Border>
</ControlTemplate>

问题是我无法在容器列表框中选择任何这些元素(HitTest 有问题)。

有什么方法可以让项目可选择或类似的东西吗?

谢谢

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    尝试按照下面的说明为 ListBox 设置 ItemsTemplate,而不是使用显式的 ControlTemplate。

    ItemsControl.ItemTemplate Property

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-12
      • 1970-01-01
      • 2010-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多