私は、項目のリストを持つComboBoxを持つUserControlを作成しようとしています。次に、何らかのアクションを実行するボタンを追加したいと思います。ボタンでUserControl ComboBoxを作成する
私はこのコードを持っている:
ComboBoxWithButton XAML:
<ComboBox x:Name="ComboBoxBtn" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="0,0,0,-1" Width="100" ItemsSource="{Binding Source}">
<ComboBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Label Content="{Binding }" Width="100" />
<Button Grid.Column="1">Something</Button>
</Grid>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
ComboBoxWithButtonコード:
public ComboBoxWithButton()
{
InitializeComponent();
}
public static readonly DependencyProperty SourceProprety =
DependencyProperty.Register("Source", typeof(ObservableCollection<object>), typeof(ComboBoxWithButton), new PropertyMetadata(null));
public ObservableCollection<object> Source
{
get { return (ObservableCollection<object>)GetValue(ValueProprety); }
set { SetValue(ValueProprety, value); }
}
その後、私のウィンドウ上の私が呼ぶ:
<controls:ComboBoxWithButton Source="{Binding SomeCollection}"/>
しかし、コンボボックスをはempですTY
"私はリスティングを取得していません"あなたはコンボボックスが空であると言っていますか?あなたのusercontrol XAMLのコンボボックスに 'ItemsSource =" {Binding Source、RelativeSource = {RelativeSource AncestorType = UserControl}} "をバインドしてみてください。 BTW 'Source'は' ObservableCollection'である必要はありません。消費者があなたに与えていることの中でできるだけ広い範囲を可能にするために非ジェネリックな 'IEnumerable'にしましょう。 'ComboBox.ItemsSource'は' IEnumerable'です。コンボボックスは物事を把握します。 –
はい、それは私が意味するものでした。あなたの言ったことを変えることはまだ同じです –
ちなみに、 'Source'プロパティを修正してみてください。 'GetValue(SourceProperty)'、 'SetValue(SourceProperty、value)'です。そこには 'ValueProperty'を使わないでください。私はそれがこの問題を引き起こしているとは思わないが、間違いなくバグだ。 –