1

我希望创建一个我称之为“AutoCompleteListBox”的控件。如果您曾经使用过 hotmail 来发送电子邮件,那么我希望创建的 to: address 行。你有一个看起来像输入框的东西,当你输入时,你会得到一个匹配对象的下拉列表。一旦您选择了一个对象(接触),它就会作为一个矩形对象添加到输入框中。可以通过这种方式添加多个对象,输入框就像一个包装面板。您可以通过退格或单击每个对象上的 x 按钮来删除对象。

我的方法是从继承 ItemsControl 开始。我已经开始编写它的控制模板,它基本上是一个包装面板,我想显示绑定的项目+一个文本框。我不知道如何将绑定项目和文本框都放在同一个换行面板中。这是我所拥有的:

<Style TargetType="ctrl:AutoCompleteListBox">
    <Setter Property="Width" Value="200"/>
    <Setter Property="Height" Value="100"/>
    <Setter Property="Background" Value="White"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ctrl:AutoCompleteListBox">
                <ScrollViewer x:Name="RootScrollViewer" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled" Padding="0" Background="{TemplateBinding Background}">
                    <toolkit:WrapPanel IsItemsHost="True">
                        <!--Items Bound To ItemSource Go Here-->
                        <TextBox x:Name="txtInput"/>
                    </toolkit:WrapPanel>
                </ScrollViewer>
            </ControlTemplate>
        </Setter.Value>
    </Setter>        
</Style>

我不知道如何表达我想要的。我知道您可以在控件模板中使用“ItemsPresenter”,它确实显示了绑定的项目,但是如何将我的文本框添加到与绑定项目相同的面板中?

我将不胜感激任何帮助。这是正确的方法吗?非常感谢。

4

1 回答 1

0

子类化项目控件是一个好的开始,但我认为控件模板的设置应该有所不同。Silverlight 工具包包含一个出色的自动完成框,您可以将其用于此目的。将其与单独的项目控件结合起来,您应该可以将其样式设置为与实时邮件“收件人”字段完全相同。

<ControlTemplate>
  <toolkit:WrapPanel>
    <ItemsControl ItemsSource="{TemplateBinding Items}">
      <ItemsControl.ItemsPanel>
        <StackPanel Orientation="Horizontal"/>
      </ItemsControl.ItemsPanel>
      <ItemsControl.ItemTemplate>
        <!-- Add data template for the previously added items here -->
      </ItemsControl.ItemTemplate>
    </ItemsControl>
    <toolkit:AutoCompleteBox ItemsSource="{TemplateBinding AutoCompleteItems}" />
  </toolkit:WrapPanel>
</ControlTemplate>
于 2010-06-13T17:33:21.910 回答