0

独自のコンボボックスを表示する新しいユーザー コントロールを作成したいと考えています。(コンボボックスには独自のスタイルやその他のものがあります.... (それは後で))

ユーザーコントロール 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>

メイン ウィンドウで、コンボボックス項目をユーザー コントロールに追加できるようにしたいと考えています。そのように:

<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 コンボボックスは、メインウィンドウで渡される項目を使用しました。

コードを実行すると、空のコンボボックスのみが表示されます

どうしたの?

4

1 に答える 1

1

これは、コンボボックスで MyItems へのバインディングを作成する方法に関係しています。

コード ビハインドで MyItems プロパティを適切にターゲットにするには、次の操作を行います。

public MyCombobox() 
{ 
    InitializeComponent(); 

    MyItems = new List<ComboBoxItem>(); 
    this.DataContext = this;
} 
于 2012-07-18T11:21:25.507 に答える