始めたばかりの進むボタンと戻るボタンを備えたカスタム コンボ ボックスを作成しようとしていますが、コンボ ボックスに一般的なリストを設定することができません。
これまでのところ、私はこれを持っています:
パブリック部分クラス NavigableComboBox : UserControl, INotifyPropertyChanged {
#region Constructor
public NavigableComboBox()
{
InitializeComponent();
}
#endregion
#region Dependency Properties
#region ListSource
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public ObservableCollection<object> ListSource
{
get
{
return (ObservableCollection<object>)GetValue(ListSourceProperty);
}
set
{
base.SetValue(NavigableComboBox.ListSourceProperty, value);
NotifyPropertyChanged("ListSource");
}
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2211:NonConstantFieldsShouldNotBeVisible")]
public static DependencyProperty ListSourceProperty = DependencyProperty.Register(
"ListSource",
typeof(ObservableCollection<object>),
typeof(NavigableComboBox),
new PropertyMetadata(OnValueChanged));
#endregion
#region ListSource
public object SelectedItem
{
get
{
return (object)GetValue(SelectedItemProperty);
}
set
{
base.SetValue(NavigableComboBox.SelectedItemProperty, value);
NotifyPropertyChanged("SelectedItem");
}
}
public static DependencyProperty SelectedItemProperty = DependencyProperty.Register(
"SelectedItem",
typeof(object),
typeof(NavigableComboBox),
new PropertyMetadata(OnValueChanged));
#endregion
#endregion
private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((NavigableComboBox)d).ListSource = (ObservableCollection<object>)e.NewValue;
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
そしてこれはxamlの場合:
<UserControl x:Class="Divestco.WinPics.Components.Common.UserControls.NavigableComboBox"
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"
xmlns:local="clr-namespace:Divestco.WinPics.Components.Common.UserControls"
mc:Ignorable="d"
d:DesignHeight="55" d:DesignWidth="200">
<Grid>
<ComboBox Name="comboBox1" ItemsSource="{Binding ListSource}" SelectedItem="{Binding SelectedItem}" />
</Grid>
</UserControl>
そして、私はそれをメインフォームでそのまま使用しています:
<UserControls:NavigableComboBox ListSource="{Binding Path=MapModel.GridsInProject}" SelectedItem="{Binding Path=MapModel.SelectedGrid}"/>
依存関係プロパティにブレーク ポイントを配置しましたが、ヒットすることはありません。カスタム コントロールではなく、通常のコンボボックスを使用すると問題が発生するため、メイン フォームのバインドが正しいことがわかります。実行時にエラーメッセージも表示されません
私が間違っていることを誰かが知っていますか?または、このような事前に作成されたコントロールを知っている人はいますか?
私が思い描いているコントロールは、コンボボックスと次の前のボタンを両側に持つものです。ユーザーは、ドロップダウン リストからアイテムを選択するか、[次へ] または [前へ] を押してスクロールできます。コントロールは、オブジェクトの任意のカスタム コレクションを取り、表示フィールドを選択できるようにする必要があります。たとえば、グリッド オブジェクトのコレクションと地平線オブジェクトのコレクションがあり、アプリの他の要因に応じて、それらのいずれかをコントロールに入力します。どちらのコレクションにも、表示フィールドとなる "Name" という名前の文字列プロパティがあります。