gpt4 book ai didi

WPF ComboBox 绑定(bind)到非字符串对象

转载 作者:行者123 更新时间:2023-12-04 19:53:19 25 4
gpt4 key购买 nike

我正在使用 MVVM (MVVM Light Toolkit),并且在 View 模型上有一个属性,它公开了一个对象列表。这些对象包含两个属性,都是字符串,它们与缩写和描述相关。我希望 ComboBox 将配对公开为“缩写 - 描述”。如果我使用数据模板,它很容易做到这一点。

我在 View 模型上有另一个属性,它表示应该显示为选中的对象——组合框中的选中项。我将 ItemsSource 绑定(bind)到列表,因为它代表可用选择的宇宙,并且正在尝试将 SelectedItem 绑定(bind)到此对象。我正在自杀,试图弄清楚为什么我不能让它工作,并且感觉更像是一个小时的骗局。

在试图了解为什么会这样的过程中,我创建了相同的方法,只使用了一个字符串列表和一个选定的字符串。这完美地工作。因此,它显然与打字有关……也许与选择平等有关?或者可能与数据模板有关?

这是 XAML:

<Window x:Class="MvvmLight1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Height="300"
Width="300"
DataContext="{Binding Main, Source={StaticResource Locator}}">

<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Skins/MainSkin.xaml" />
</ResourceDictionary.MergedDictionaries>
<DataTemplate x:Key="DataTemplate1">
<StackPanel Orientation="Horizontal">
<TextBlock TextWrapping="Wrap" Text="{Binding CourtCode}"/>
<TextBlock TextWrapping="Wrap" Text=" - "/>
<TextBlock TextWrapping="Wrap" Text="{Binding CourtDescription}"/>
</StackPanel>
</DataTemplate>
</ResourceDictionary>
</Window.Resources>

<Grid x:Name="LayoutRoot">
<ComboBox x:Name="cmbAbbrevDescriptions" Height="35" Margin="25,75,25,25" VerticalAlignment="Top" ItemsSource="{Binding Codes}" ItemTemplate="{DynamicResource DataTemplate1}" SelectedItem="{Binding selectedCode}" />
<ComboBox x:Name="cmbStrings" Height="35" Margin="25" VerticalAlignment="Top" ItemsSource="{Binding strs}" SelectedItem="{Binding selectedStr}"/>
</Grid>
</Window>

而且,如果有帮助,这里是 ViewModel:
using GalaSoft.MvvmLight;
using MvvmLight1.Model;
using System.Collections.Generic;

namespace MvvmLight1.ViewModel
{
public class MainViewModel : ViewModelBase
{
public const string CodesPropertyName = "Codes";
private List<Court> _codes = null;
public List<Court> Codes
{
get
{
return _codes;
}

set
{
if (_codes == value)
{
return;
}

var oldValue = _codes;
_codes = value;

// Update bindings and broadcast change using GalaSoft.Utility.Messenging
RaisePropertyChanged(CodesPropertyName, oldValue, value, true);
}
}

public const string selectedCodePropertyName = "selectedCode";
private Court _selectedCode = null;
public Court selectedCode
{
get
{
return _selectedCode;
}

set
{
if (_selectedCode == value)
{
return;
}

var oldValue = _selectedCode;
_selectedCode = value;

// Update bindings and broadcast change using GalaSoft.Utility.Messenging
RaisePropertyChanged(selectedCodePropertyName, oldValue, value, true);
}
}

public const string strsPropertyName = "strs";
private List<string> _strs = null;
public List<string> strs
{
get
{
return _strs;
}

set
{
if (_strs == value)
{
return;
}

var oldValue = _strs;
_strs = value;

// Update bindings and broadcast change using GalaSoft.Utility.Messenging
RaisePropertyChanged(strsPropertyName, oldValue, value, true);
}
}

public const string selectedStrPropertyName = "selectedStr";
private string _selectedStr = "";
public string selectedStr
{
get
{
return _selectedStr;
}

set
{
if (_selectedStr == value)
{
return;
}

var oldValue = _selectedStr;
_selectedStr = value;

// Update bindings and broadcast change using GalaSoft.Utility.Messenging
RaisePropertyChanged(selectedStrPropertyName, oldValue, value, true);
}
}


/// <summary>
/// Initializes a new instance of the MainViewModel class.
/// </summary>
public MainViewModel()
{
Codes = new List<Court>();

Court code1 = new Court();
code1.CourtCode = "ABC";
code1.CourtDescription = "A Court";

Court code2 = new Court();
code2.CourtCode = "DEF";
code2.CourtDescription = "Second Court";

Codes.Add(code1);
Codes.Add(code2);


Court code3 = new Court();
code3.CourtCode = "DEF";
code3.CourtDescription = "Second Court";
selectedCode = code3;

selectedStr = "Hello";
strs = new List<string>();
strs.Add("Goodbye");
strs.Add("Hello");
strs.Add("Ciao");

}
}
}

这是暴露的可笑的微不足道的类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MvvmLight1.Model
{
public class Court
{
public string CourtCode { get; set; }

public string CourtDescription { get; set; }
}
}

谢谢!

最佳答案

运行时不知道 code2 和 code3 应该相等。

http://msdn.microsoft.com/en-us/library/ms173147(VS.80).aspx

public override bool Equals(object o)
{
var court = o as Court;
if(court == null)
return false;
return CourtCode == court.CourtCode;
}

关于WPF ComboBox 绑定(bind)到非字符串对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2986636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com