これはしばらくの間私を困惑させてきました。私は a ComboBox
、HierarchicalDataTemplate
in a TreeView
(... in a UserControl ) に a を持っています - これは機能SELECT
します。つまり、データベースで a を実行して、正しい値が保存されたことを確認できます。
問題は、データベースからデータをロードして表示するときです (申し訳ありませんが、フランス語のロケール)。
...しかし、ドロップダウンリストには期待されるすべての値が含まれており、いずれかを選択すると、選択した値が正しく表示されます:
変更を保存すると、正しいことをしたことがわかります。
...しかし、データをリロードすると、すべてが A-1、トップシェイプ、完璧です... ViewModel の完全修飾型名を表示し続けるこの頑固な小さな ComboBox を除いて...
欠陥のある ComboBox のマークアップは次のとおりです。正直なところ、何も問題はないと思います。
<ComboBox Style="{StaticResource StdDropdown}"
IsEditable="True"
TextSearch.TextPath="Value"
ItemsSource="{Binding SelectedOption.Values}"
SelectedItem="{Binding SelectedValue, Mode=TwoWay}">
<ComboBox.ItemTemplate>
<DataTemplate DataType="viewModels:POptionValueViewModel">
<Border Style="{StaticResource ListItemBorder}">
<StackPanel Orientation="Horizontal">
<Label Style="{StaticResource DropdownLabel}"
Content="{Binding Value}" />
</StackPanel>
</Border>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
これがどのように可能であるかを知る必要があります (そして、それを修正する方法も!)。つまり、リスト全体に ViewModel.ToString() が表示されているわけではありません...
SelectedValue
解決策は、代わりにバインドすることですSelectedItem
:
<ComboBox Style="{StaticResource StdDropdown}"
IsEditable="True"
TextSearch.TextPath="Value"
ItemsSource="{Binding SelectedOption.Values}"
SelectedValue="{Binding SelectedValue, Mode=TwoWay}">
完全を期すために(そして、一方が機能するのに他方が機能しない理由を理解するのに役立つ場合)-表示されるViewModel:
/// <summary>
/// Encapsulates a <see cref="IPOptionValue"/> implementation for presentation purposes.
/// </summary>
[ComVisible(false)]
public class POptionValueViewModel : ViewModelBase<IPOptionValue>
{
public POptionValueViewModel(IPOptionValue entity) : base(entity) { }
public string OptionName { get { return Entity.POptionName; } }
/// <summary>
/// Gets a number representing the sorting order for the P-Option value.
/// </summary>
/// <value>
/// The sort order.
/// </value>
public int SortOrder { get { return Entity.SortOrder; } }
/// <summary>
/// Gets the P-Option value.
/// </summary>
/// <value>
/// The value.
/// </value>
public string Value { get { return Entity.Value.Trim(); } }
public override bool IsNew
{
get { return false; }
}
}
...そして TreeView アイテムの ViewModel プロパティ:
public POptionValueViewModel SelectedValue
{
get
{
var result = SelectedOption == null
? null
: SelectedOption.Values.SingleOrDefault(e => e.Value == Entity.POptionValue.Trim());
return result;
}
set
{
var selectedOption = _parentGroupOptions.SingleOrDefault(option => option.Name == value.OptionName);
if (selectedOption == null) return;
var selectedValue = selectedOption.Values.SingleOrDefault(option => option.Value.Trim() == value.Value.Trim());
if (selectedValue == null) return;
Entity.POptionValue = selectedValue.Value.Trim();
NotifyPropertyChanged(() => SelectedValue);
}
}