2009-09-29 61 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> 

可以用類型轉換器,以支持添加任意東西的收集和依賴屬性支持綁定擴展這個樣本,但這應該讓你開始。

希望它有幫助!