ObjectDataProviderにはObjectTypeプロパティがあります。コンボボックスのデータソースとして、私のビューモデルのEnumプロパティを使用したいと思っています。この場合 オブジェクトタイプは次のとおりmscorlibアセンブリ内System名前空間に存在
System.Enum
。 従って、我々は輸入私たちのXAML名前空間を追加する必要があります。
のxmlns:SYS =「CLR名前空間:システム、アセンブリ= mscorlib」
ここで私が参照するために全体のコードを掲載します。
Test.xaml(ウィンドウ):
<Window x:Class="WpfTrash.Test"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfTrash"
-->> xmlns:sys="clr-namespace:System;assembly=mscorlib" <<--
mc:Ignorable="d"
Title="Test" Height="300" Width="300">
<Window.Resources>
<local:MyEnumToStringConverter x:Key="MyEnumConverter" />
<ObjectDataProvider x:Key="odp" MethodName="GetNames" ObjectType="{x:Type sys:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="local:TheEnum"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
<StackPanel>
<TextBox x:Name="txtTheInt" Text="{Binding Path=TheInt}"/>
<TextBox x:Name="txtTheString" Text="{Binding Path=TheString}"/>
<!--<ComboBox x:Name="cboTheEnum" DataContext="{StaticResource SortedEnumView}" ItemsSource="{Binding}"/>-->
<ComboBox x:Name="cboTheEnum" ItemsSource="{Binding Source={StaticResource odp}}" SelectedValue="{Binding Path=TheEnum, Converter={StaticResource MyEnumConverter}}"/>
<Button x:Name="button" Content="Button" Click="button_Click"/>
</StackPanel>
</Window>
T est.xaml.cs
namespace WpfTrash
{
/// <summary>
/// Interaction logic for Test.xaml
/// </summary>
public partial class Test : Window
{
MyClass vm = new MyClass();
public Test()
{
InitializeComponent();
this.DataContext = vm;
}
private void button_Click(object sender, RoutedEventArgs e)
{
var selecteditem = (TheEnum)Enum.Parse(typeof(TheEnum), cboTheEnum.SelectedItem.ToString(), true);
}
}
}
のviewmodel:
public class MyClass : INotifyPropertyChanged
{
public int TheInt { get; set; }
public string TheString { get; set; }
TheEnum theEnum;
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public TheEnum TheEnum
{
get
{
return theEnum;
}
set
{
theEnum = value;
NotifyPropertyChanged("TheEnum");
}
}
}
列挙定義:(クラス定義の一部ではない)
public enum TheEnum
{
A, B, C, D
}
コンバータ:
'sysml'名前空間を' xmlns: '属性で宣言しましたか?問題を確実に再現する良い[mcve]を提供してください。ほとんどの場合、それはある種の誤植ですが、良いMCVEがなければ、あなたが間違いを犯したことを正確に知ることさえできません。 –
私はsysの問題を解決しました。私の主な問題はtwowayバインディングです。私はすぐに例を作成してアップロードします... – Legends
テキストボックスにいくつかの文字を入力すると、それらは即座にviewmodelに更新されます。そして、テキストボックスのviewmodelプロパティを更新すると、ビューはそれ以上のコーディングなしで更新されます。 viewmodelのenumプロパティからその値を取得するコンボボックスと同じもの – Legends