WPFのComboBoxでMultiBindingのを使用する方法
-
25-09-2019 - |
質問
このはNUTS私を運転している!!!
私は罰金を動作しますが、従業員のみ最初の名前を表示する従業員がクエリをフィルタするために使用ComboBox
を持っています。私は従業員のフルネームを表示するためにMultiValueConverter
を使用したい(私たちは2マイクスと2 DAVESを持っていなかった場合、これは緊急性の低いでしょう)
以下は、私の作業コード及び(簡潔にするために出てトリミング不要の書式付き)IMultiValueConverter
クラスです。私は仕事に複数のコンバーを取得するために、私は考えることができるすべてのものを試してみましたが、私は運がなかったています。
<ComboBox ItemsSource="{Binding Path=EmployeesFilter}"
DisplayMemberPath="EmpFirstName"
SelectedValue="{Binding Path=EmployeeToShow, Mode=TwoWay}"/>
のViewModelのプロパティは、それがバインドされます:
// This collection is used to populate the Employee Filter ComboBox
private ObservableCollection<Employee> employeesFilter;
public ObservableCollection<Employee> EmployeesFilter
{
get {
return employeesFilter;
}
set {
if (employeesFilter != value)
{
employeesFilter = value;
OnPropertyChanged("EmployeesFilter");
}
}
}
// This property is TwoWay bound to the EmployeeFilters SelectedValue
private Employee employeeToShow;
public Employee EmployeeToShow
{
get {
return employeeToShow;
}
set {
if (employeeToShow != value)
{
employeeToShow = value;
OnPropertyChanged("EmployeeToShow");
QueryIssues(); // Requery with new employee filter
}
}
}
IMultiValueConverterます:
class StringsToFullNameMultiConverter : IMultiValueConverter
{
public object Convert(object[] values,
Type targetType,
object parameter,
System.Globalization.CultureInfo culture)
{
// I added this because I kept getting DependecyProperty.UnsetValue
// Passed in as the program initializes
if (values[0] as string != null)
{
string firstName = (string)values[0];
string lastName = (string)values[1];
string fullName = firstName + " " + lastName;
return fullName;
}
return null;
}
public object[] ConvertBack(object value,
Type[] targetTypes,
object parameter,
System.Globalization.CultureInfo culture)
{
return null;
}
}
私は別の多くのことを試みたが、基本的には、
コンボボックスで次のように使用しています<ComboBox.SelectedValue>
<MultiBinding Converter="{StaticResource StringsToFullNameMultiConverter}"
Mode="OneWay" >
<Binding Path="EmpFirstName" />
<Binding Path="EmpLastName"/>
</MultiBinding>
</ComboBox.SelectedValue>
それが今立つようプログラムがDependencyProperty.UnsetValue
に設定された値で初期化したときにコンバータが呼び出されます。その後、あなたがボックスから名前を選択した場合でも、再び呼び出されることはありません。名前はまだ最初の名前として表示されます。
あなたが提供することができます良いチュートリアル/サンプルに任意のヘルプまたはポインタをありがとう。私はウェブ上で見つけ続けるすべてのものは、テキストボックスのためのものであると私は一日中うまくそれらを使用することができます。
解決
あなたは近いです!何あなたががやりたいことはComboBox.ItemTemplate
、ないSelectedValue
です。いくつかのXAMLの地獄のために準備します。
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource StringsToFillNameMultiConverter}">
<Binding Path="EmpFirstName" />
<Binding Path="EmpLastName" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
私が正しくリコール場合は、また、あなたはあなただけの文字列をフォーマットしている場合は、独自のコンバータを作成する必要はありません。私は、あなたが次の操作を行うことができると思います(私が間違っている場合、誰かが私を修正してください。)
<!-- "Last, First" -->
<MultiBinding StringFormat="{}{1}, {0}">
<Binding Path="EmpFirstName" />
<Binding Path="EmpLastName" />
</MultiBinding>
他のヒント
あなたは、アイテムのためにデータテンプレートを使用する方が良いかもしれません。
型変換はOKでは、さまざまなフィールドの書式設定を制御する必要性を持っていない提供されます。
私は私のクラスにReadOnlyプロパティを追加することで終わったとコンボボックスでDisplaymemberpathを使用する
public class MyEmployee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string DisplayName {
get { return FirstName + " " + LastName; }
}
}
...あなたの状況については、この作品のようなものをもらえますか? BR、 ダニエル