簡體   English   中英

創建一個組合框用戶控件,並從主窗口傳遞項目

[英]Create a Combobox User Control and pass the items from the mainwindow

我想創建一個新的用戶控件,該控件顯示我自己的組合框。 (組合框具有自己的樣式和其他功能。...(稍后介紹))

usercontrol 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>

文件后面的用戶控制代碼:

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

            MyItems = new List<ComboBoxItem>();
        }

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

主窗口xaml文件:

<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添加到我的用戶控件中。 像那樣:

<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組合框使用了在主窗口中傳遞的項目。

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

怎么了?

這與您在組合框中創建綁定到MyItems的方式有關。

若要使其正確地定位在后面的代碼中的MyItems屬性,可以執行以下操作:

public MyCombobox() 
{ 
    InitializeComponent(); 

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM