gpt4 book ai didi

Silverlight Combobox - 设置 SelectedItem MVVM

转载 作者:行者123 更新时间:2023-12-03 10:30:32 25 4
gpt4 key购买 nike

我有一个 ViewModel 设置“UserStructure”属性的值。问题是组合框不会绑定(bind)到该值。

public class OwnerOccupierAccountViewModel : ViewModelBase
{
/// <summary>
/// Load combobox structures
/// </summary>
private readonly LoadOperation<Structure> _loadStructures;

private readonly LoadOperation<UnitOccupierDetail> _loadUnitOccupierDetails;

//public ICommand SaveAccountSettingsCommand { get; set; }

#region Properties

private ObservableCollection<Structure> _structures;
public ObservableCollection<Structure> Structures
{
get { return _structures; }

set
{
_structures = value;
RaisePropertyChanged("Structures");
}
}

private Structure _userStructure;
public Structure UserStructure
{
get { return _userStructure; }
set
{
_userStructure = value;
RaisePropertyChanged("SelectedStructure");
}
}

private UnitOccupierDetail _unitOccupierDetail;
public UnitOccupierDetail UnitOccupierDetail
{
get { return _unitOccupierDetail; }
set
{
_unitOccupierDetail = value;
RaisePropertyChanged("UnitOccupierDetail");
}
}


#endregion

public OwnerOccupierAccountViewModel()
{
// SaveAccountSettingsCommand = new DelegateCommand(SaveAccountSettings, CanSave);

UserAccountContext _userAccountContext;

if (!DesignerProperties.IsInDesignTool)
{
var loggedInUser = new Guid(WebContext.Current.User.UserID.ToString());

_userAccountContext = new UserAccountContext();

#region load structures
_loadStructures = _userAccountContext.Load(_userAccountContext.GetStructuresQuery());
_loadStructures.Completed += _loadStructuresCompleted;
#endregion

#region load user data
_loadUnitOccupierDetails =
_userAccountContext.Load(
_userAccountContext.GetUnitOccupierDetailsQuery().Where(
u => u.UserIDFK == loggedInUser && u.StructureFK == 92));
_loadUnitOccupierDetails.Completed += _loadUnitOccupierDetails_Completed;
#endregion
}
}

void _loadUnitOccupierDetails_Completed(object sender, EventArgs e)
{
_unitOccupierDetail= new UnitOccupierDetail();
_unitOccupierDetail = _loadUnitOccupierDetails.Entities.First();

_userStructure = _unitOccupierDetail.Structure;
}

void _loadStructuresCompleted(object sender, EventArgs e)
{
var theseStructures = new ObservableCollection<Structure>(_loadStructures.Entities);
Structures = theseStructures;
}

//private void SaveAccountSettings(object param)
//{

//}

//private static bool CanSave(object param)
//{
// return true;
//}
}

<ComboBox x:Name="cboApartments"
ItemsSource='{Binding Structures, Mode=TwoWay}'
DisplayMemberPath='StructureName'
SelectedValuePath='IDStructure'
SelectedItem='{Binding SelectedStructure,Mode=TwoWay}'
Width="200"
Height="25">

最佳答案

在 xaml UserStructure 中,而不是 SelectedStructure。

关于Silverlight Combobox - 设置 SelectedItem MVVM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5625126/

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