如何在Silverlight中为列表框项选择自定义数据模板中的UI元素

时间:2011-07-25 22:38:20

标签: silverlight listbox

大家好,我有一个已定义为数据模板的usercontrol。我想要做的是在一个包装面板中返回结果,并以tile格式返回每个结果。我这一切都正常工作,并且正确地返回了结果。但是,我希望用户点击数据模板项目。 (滚动查看器滚动数据,按钮单击和文本选择)/当前单击选定项目时,项目被选中,但就好像列表框项目中的所有内容都被锁定(不可选)。

我很感激有关我在这里缺少什么的建议。下面列出的是我的用户控件代码以及我如何从app.xaml

引用包装面板

SearchResultTileControl.xaml

<UserControl x:Class="UI.Search.Controls.SearchResultTileControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:dts="clr-namespace:UI.Search.Commands"
         xmlns:formatter="clr-namespace:UI.Search.Commands"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:qr="clr-namespace:UI.Search.Controls.tiles"
         d:DesignHeight="300"
         d:DesignWidth="400"
         mc:Ignorable="d">

<Grid x:Name="LayoutRoot"  >
    <ListBox x:Name="ResultListBox"
             HorizontalAlignment="Stretch"
             Background="{x:Null}"
             BorderThickness="0"
             HorizontalContentAlignment="Stretch"
             ItemsPanel="{StaticResource ResultsItemsControlPanelTemplate}"
             ItemContainerStyle="{StaticResource ListBoxItemStyle1}"
             ItemsSource="{Binding SearchResults[0].Results}"
             ScrollViewer.HorizontalScrollBarVisibility="Disabled">
        <ListBox.ItemTemplate>

            <DataTemplate>
                <formatter:TypeTemplateSelector Content="{Binding}" HorizontalContentAlignment="Stretch">   
                    <!--  Person Template  -->
                    <formatter:TypeTemplateSelector.PersonTemplate>
                        <DataTemplate>
                            <qr:ucTilePerson />
                        </DataTemplate>
                    </formatter:TypeTemplateSelector.PersonTemplate>

                    <!--  Incident Template  -->
                    <formatter:TypeTemplateSelector.IncidentTemplate>
                        <DataTemplate>
                            <qr:ucTileIncident />
                        </DataTemplate>
                    </formatter:TypeTemplateSelector.IncidentTemplate>

                </formatter:TypeTemplateSelector>

            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>

</Grid>

在Usercontrol ucTilePerson.xaml中,我将模板设置为:

<UserControl x:Class="UI.Search.Controls.tiles.ucTilePerson"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:formatter="clr-namespace:UI.Search.Commands"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"
         Width="300"
         Height="250"
         d:DesignHeight="250"
         d:DesignWidth="300"
         IsHitTestVisible="False"
         mc:Ignorable="d">
<UserControl.Resources>
    <formatter:TileHighlightConverter x:Key="FormatConverter" />
</UserControl.Resources>
<Grid x:Name="PersonLayoutRoot">
    <Rectangle Style="{StaticResource TileBackground}" />
    <ScrollViewer Margin="5" BorderBrush="{x:Null}">
        <StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
            <StackPanel Margin="0,0,0,2" Orientation="Horizontal">
                <StackPanel>
                    <Image Width="48"
                           Height="48"
                           Source="/Images/search/person.png" />
                    <TextBlock Style="{StaticResource TileRelevance}" Text="{Binding Relevance}" />
                </StackPanel>
                <StackPanel>
                    <HyperlinkButton Content="{Binding Type}" Style="{StaticResource TypeHyperlinkButton}" />
                    <TextBox Margin="0,0,0,2"
                             Style="{StaticResource TileTextBox}"
                             Text="{Binding Content[AgencyName]}"
                             TextWrapping="Wrap" />
                </StackPanel>
            </StackPanel>

            <toolkit:WrapPanel Margin="0,0,0,2">
                <TextBlock Style="{StaticResource TileLabel}" Text="Name" />
                <TextBox Margin="0,0,3,0"
                         Style="{StaticResource TileTextBox}"
                         Text="{Binding Content[lastname]}" />
                <TextBox Margin="0,0,3,0"
                         Style="{StaticResource TileTextBox}"
                         Text="{Binding Content[firstname]}" />
                <TextBox Style="{StaticResource TileTextBox}" Text="{Binding Content[middlename]}" />
            </toolkit:WrapPanel>
            <Border Style="{StaticResource TileBorder}">
                <toolkit:WrapPanel Orientation="Horizontal">
                    <StackPanel Style="{StaticResource TileVerticalStackPanel}">
                        <TextBlock Style="{StaticResource TileLabel}" Text="Race" />
                        <TextBox Style="{StaticResource TileTextBox}" Text="{Binding Content[race]}" />
                    </StackPanel>
                    <StackPanel Style="{StaticResource TileVerticalStackPanel}">
                        <TextBlock Style="{StaticResource TileLabel}" Text="Sex" />
                        <TextBox Style="{StaticResource TileTextBox}" Text="{Binding Content[sex]}" />
                    </StackPanel>
                    <StackPanel Style="{StaticResource TileVerticalStackPanel}">
                        <TextBlock Style="{StaticResource TileLabel}" Text="DOB" />
                        <TextBox Style="{StaticResource TileTextBox}" Text="{Binding Content[dob]}" />
                    </StackPanel>
                </toolkit:WrapPanel>
            </Border>
            <Border Style="{StaticResource TileBorder}">
                <toolkit:WrapPanel Orientation="Horizontal">
                    <StackPanel Style="{StaticResource TileVerticalStackPanel}">
                        <TextBlock Style="{StaticResource TileLabel}" Text="Involvement" />
                        <TextBox Style="{StaticResource TileTextBox}" Text="{Binding Content[involvementtype]}" />
                    </StackPanel>
                    <StackPanel Style="{StaticResource TileVerticalStackPanel}">
                        <TextBlock Style="{StaticResource TileLabel}" Text="Associated Event" />
                        <TextBox Style="{StaticResource TileTextBox}" Text="{Binding Content[0].EventAssociation}" />
                        <HyperlinkButton Content="{Binding Content[0].EventID}" Style="{StaticResource TileResultLink}" />
                    </StackPanel>

                </toolkit:WrapPanel>
            </Border>
            <Border Style="{StaticResource TileBorder}">
                <ContentControl Width="256"
                                Margin="0,0,6,0"
                                BorderThickness="0"
                                Content="{Binding HitContext,
                                                  Converter={StaticResource FormatConverter}}"
                                FontSize="11" />
            </Border>

        </StackPanel>
    </ScrollViewer>
</Grid>

然后我设置了对app.xaml中listboxt ItemsPanel中使用的包装面板的引用

<ItemsPanelTemplate x:Key="ResultsItemsControlPanelTemplate">
        <toolkit:WrapPanel/>
    </ItemsPanelTemplate>

我怀疑这是列表框样式中可能阻止这种情况的东西,但我并不积极。 再次感谢您的任何建议,

干杯

1 个答案:

答案 0 :(得分:0)

发现了这个问题。在usercontrol(ucTilePerson)中,我将IsHitTestVisible设置为false。由于它是在用户控制级别设置的,所有元素都继承了这个属性,这就是为什么我无法在任何事情上引发任何鼠标事件的原因。

不知道为什么我把它设置在当天晚些时候。

干杯