2012-07-18 25 views
0

我想要創建一個新的用戶控件來顯示我自己的組合框。 (組合框有自己的風格和其他的東西......(是後話))創建組合框用戶控件並從主窗口中傳遞項目

用戶控件XAML文件:

namespace WpfApplication1 
{ 
    public partial class MyCombobox 
    { 
     public MyCombobox() 
     { 
      InitializeComponent(); 

      MyItems = new List<ComboBoxItem>(); 
     } 

     public List<ComboBoxItem> MyItems { get; set; } 
    } 
} 

主窗口XAML文件:隱藏文件

<UserControl x:Class="WpfApplication1.MyCombobox" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" d:DesignWidth="500" 
      Height="50"> 

    <ComboBox Height="42" ItemsSource="{Binding Path=MyItems}"></ComboBox> 

</UserControl> 

用戶控件代碼:

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:WpfApplication1="clr-namespace:WpfApplication1" WindowStartupLocation="CenterScreen" 
     Height="350" 
     Width="500" 
     > 
    <Grid> 
     <WpfApplication1:MyCombobox> 
      <WpfApplication1:MyCombobox.MyItems> 
       <ComboBoxItem Height="36"> 
        <Grid> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition Width="30"/> 
          <ColumnDefinition Width="*"/> 
         </Grid.ColumnDefinitions> 
         <Grid Grid.Column="1"> 
          <Grid.RowDefinitions> 
           <RowDefinition Height="16" /> 
           <RowDefinition Height="16" /> 
          </Grid.RowDefinitions> 
          <TextBlock Text="Item Title 1" Grid.Row="0" FontWeight="Bold" /> 
          <TextBlock Text="Item Description 1" Grid.Row="1" FontStyle="Italic" /> 
         </Grid> 
        </Grid> 
       </ComboBoxItem> 
       <ComboBoxItem Height="36"> 
        <Grid> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition Width="30"/> 
          <ColumnDefinition Width="*"/> 
         </Grid.ColumnDefinitions> 
         <Grid Grid.Column="1"> 
          <Grid.RowDefinitions> 
           <RowDefinition Height="16" /> 
           <RowDefinition Height="16" /> 
          </Grid.RowDefinitions> 
          <TextBlock Text="Item Title 2" Grid.Row="0" FontWeight="Bold" /> 
          <TextBlock Text="Item Description 2" Grid.Row="1" FontStyle="Italic" /> 
         </Grid> 
        </Grid> 
       </ComboBoxItem> 
      </WpfApplication1:MyCombobox.MyItems> 
     </WpfApplication1:MyCombobox> 
    </Grid> 
</Window> 

我希望我可以添加,在主窗口,comboboxitems我的usercontrol。像這樣:

<WpfApplication1:MyCombobox.MyItems> 
    <ComboBoxItem Height="36"> 
     <Grid> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="30"/> 
       <ColumnDefinition Width="*"/> 
      </Grid.ColumnDefinitions> 
      <Grid Grid.Column="1"> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="16" /> 
        <RowDefinition Height="16" /> 
       </Grid.RowDefinitions> 
       <TextBlock Text="Item Title 2" Grid.Row="0" FontWeight="Bold" /> 
       <TextBlock Text="Item Description 2" Grid.Row="1" FontStyle="Italic" /> 
      </Grid> 
     </Grid> 
    </ComboBoxItem> 
    <!-- more items... --> 
</WpfApplication1:MyCombobox.MyItems> 

UserControl組合框使用在主窗口中傳遞的項目。

當我運行的代碼,它只是顯示一個空的組合框

有什麼不對?

+0

嘗試使用的ObservableCollection爲MyItems – JleruOHeP 2012-07-18 10:48:40

+0

@JleruOHeP它改變不了什麼=( – David 2012-07-18 10:54:12

回答

1

這與您在Combobox中創建與MyItems綁定的方式有關。

,使其正常瞄準MyItems財產隱藏代碼,你可以做到以下幾點:

public MyCombobox() 
{ 
    InitializeComponent(); 

    MyItems = new List<ComboBoxItem>(); 
    this.DataContext = this; 
} 
+0

如果列表中的ObservableCollection,因爲它實現了PropertyChange?或者它有什麼關係? – 2012-07-18 11:56:26

+1

在這種情況下,它並不重要,因爲這些項目在數據綁定發生之前都已經添加了,但是如果應該可以在程序執行期間從MyList添加/具有反映在Combobox中的變化,它必須是ObservableCollection才能工作。 – 2012-07-18 12:17:17

+0

A h,很酷。很高興知道,謝謝! – 2012-07-18 12:23:46

相關問題