2009-09-29 64 views
0

我已经创建了我插入了我的窗前用下面的代码允许将元素添加到列表中的XAML

<controls:ListExpander Text="Class Diagrams"></controls:ListExpander> 

有问题的控制自定义的控件包含几个子控件等等,清单。如何创建设置,以便我可以指定应添加到列表中的项目?最后,我在寻找以下架构

<controls:ListExpander Text="Class Diagrams"> 
    <SomeItem>data<SomeItem> 
    <SomeItem>data<SomeItem> 
    <SomeItem>data<SomeItem> 
    <SomeItem>data<SomeItem> 
</controls:ListExpander> 

在这种情况下,SomeItem对象应该被添加到列表中ListExpander:

<ListBox Name="lstItems" Background="LightGray"> 
     <ListBox.Items> 
      // Items should go here 
     </ListBox.Items> 
    </ListBox> 

我很新的WPF,但我想这是沿着在ListExpander上创建一个依赖关系集合的东西,它需要SomeItem类型的对象?

编辑:让我澄清一下。我只是希望能够给控件一些参数,它可以转换为控件中包含的列表框中的项目。

回答

1

您是否为ListExpander创建了类?您可以非常简单地继承ItemsControl的所有功能。这里是我为你创建的一个示例类,以便对它进行建模。

CLASS

using System.Windows.Controls; 

namespace ListExpanderSample 
{ 
    public class ListExpander : ItemsControl 
    { 
     static ListExpander() 
     { 
      /* Required logic goes here */ 
     } 
    } 
} 

实施

<Grid> 
    <Grid.Resources> 
     <Style x:Key="{x:Type local:ListExpander}" TargetType="{x:Type local:ListExpander}"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type local:ListExpander}"> 
         <Border Background="Transparent" > 
          <ScrollViewer Focusable="False" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto"> 
           <ItemsPresenter /> 
          </ScrollViewer> 
         </Border> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </Grid.Resources> 
    <local:ListExpander> 
     <local:ListExpander.Items> 
      <Button Content="Button 1"/> 
      <TextBox Text="TextBox 1"/> 
      <Button Content="Button 2"/> 
      <TextBox Text="TextBox 2"/> 
     </local:ListExpander.Items> 
    </local:ListExpander> 
</Grid> 

SCREENSHOT

Sample ListExpander http://lh4.ggpht.com/_jvamiP47SsA/SsWM6xN_3BI/AAAAAAAAAkg/tZUWxzi9wVI/s800/Window1.jpg

我希望这能帮助你,Qua。

1

我假设你的控件包含的不仅仅是你想添加的这些孩子,对吧?所以我会通过增加一个属性控件类像这样去:

private ObservableCollection<UIElement> _items = new ObservableCollection<UIElement>(); 
public ObservableCollection<UIElement> Items 
{ 
    get { return this._items; } 
} 
在你的控制

然后模板,你只是你的列表框绑定到该集合

<ListBox Name="lstItems" Background="LightGray" ItemsSource="{Binding Items}"> 
</ListBox> 

这样,你就可以使用这样的:

<controls:ListExpander Text="Class Diagrams"> 
    <controls:ListExpander.Items> 
    <SomeItem>data<SomeItem> 
    <SomeItem>data<SomeItem> 
    <SomeItem>data<SomeItem> 
    <SomeItem>data<SomeItem> 
    </controls:ListExpander.Items> 
</controls:ListExpander> 

为了能够使用它,而不<controls:ListExpander.Items>[ContentProperty("Items")]属性装饰你的类。

+0

Items和lstItems之间的绑定似乎不起作用。 SomeItem对象实际上被添加到_items,但不是列表框。 – 2009-10-03 19:48:03

+0

也许你需要在你的控件构造函数中设置DataContext来绑定工作。类似this.DataContext = this;或者使用TemplateBinding而不是Binding,或者使用指定RelativeSource的较长版本的Binding表达式。无论如何,原则的立场,你只需要在我的示例代码中找到一个缺少的怪癖。 – 2009-10-09 15:49:51

0

有一个简单的方法来做到这一点。类型ItemCollection的属性添加到您的控件类是这样的:

public ItemCollection Items{ 
    get{ 
     return innerItems.Items; 
    }set{ 
     innerItems.Items.Clear(); 
     foreach (var item in value){ 
      innerItems.Items.Add(item); 
     } 
    } 
} 

其中innerItems只是在你的XAML一个ItemsControl(像这样):

<UserControl x:Class="TestApp.ItemsExtender" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Height="300" Width="300"> 
    <StackPanel> 
     <Label Content="Control Label" /> 
     <ItemsControl x:Name="innerItems" /> 
    </StackPanel> 
</UserControl> 

然后将属性添加到喜欢你的控件类这样的:

[ContentProperty("Items")] 
public partial class ItemsExtender : UserControl 
{...} 

这将告诉XAML是默认的项目应分配给您的项目属性,你的项目属性将使用它们来填充您的内部列表收藏。然后,您可以使用它像这样:

<this:ItemsExtender> 
    <Label Content="item1" /> 
    <Label Content="item2" /> 
    <Label Content="item3" /> 
</this:ItemsExtender> 

可以用类型转换器,以支持添加任意东西的收集和依赖属性支持绑定扩展这个样本,但这应该让你开始。

希望它有帮助!