C# WPF数据绑定有问题吗
我有两个对象,一个客户和一个商店。有多个门店,每个客户都有一个名为PreferredStoreId(int?)的属性,该属性与门店Id(int?)相关 在WPF应用程序中,我试图构建一个允许编辑客户的表单。此表单上存在一个组合框,其中填充了存储,用作显示当前设置的PreferredStore和更改首选存储的方式 我的问题是,虽然我可以填充combobox,但我无法在Customer.PreferredId(设置为UserControl的datacontext的对象)和combobox的SelectedItem(存储对象)的.Id属性之间获得双向绑定 以下是我的XAML,有助于理解:C# WPF数据绑定有问题吗,c#,wpf,xaml,data-binding,combobox,C#,Wpf,Xaml,Data Binding,Combobox,我有两个对象,一个客户和一个商店。有多个门店,每个客户都有一个名为PreferredStoreId(int?)的属性,该属性与门店Id(int?)相关 在WPF应用程序中,我试图构建一个允许编辑客户的表单。此表单上存在一个组合框,其中填充了存储,用作显示当前设置的PreferredStore和更改首选存储的方式 我的问题是,虽然我可以填充combobox,但我无法在Customer.PreferredId(设置为UserControl的datacontext的对象)和combobox的Selec
<UserControl x:Class="ucCustomerEditor"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:localViewModels="clr-namespace:ViewModels"
xmlns:qc="clr-namespace:QuickConverter;assembly=QuickConverter"
mc:Ignorable="d" d:DesignWidth="750" Height="334">
<UserControl.DataContext>
<localViewModels:CustomerViewModel x:Name="customerViewModel" />
</UserControl.DataContext>
<StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Button Height="26" Width="50" Content="Save" Margin="5,10" Click="UserAction_Save" />
<Button Height="26" Width="50" Content="Cancel" Margin="10,10" Click="UserAction_Cancel" />
</StackPanel>
<Grid Height="26" Margin="10" VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="209"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBox Text="{Binding FirstName}" Height="23" Margin="10,0,0,0" TextWrapping="Wrap" VerticalAlignment="Top" HorizontalAlignment="Stretch" Grid.Column="1"/>
<Label Content="First Name:" Margin="10,0" VerticalAlignment="Top" FontWeight="Bold"/>
</Grid>
<Grid Height="26" Margin="10" VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="209"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBox Text="{Binding LastName}" Height="23" Margin="10,0,0,0" TextWrapping="Wrap" VerticalAlignment="Top" HorizontalAlignment="Stretch" Grid.Column="1"/>
<Label Content="Last Name:" Margin="10,0" VerticalAlignment="Top" FontWeight="Bold"/>
</Grid>
<Grid Height="26" Margin="10" VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="209"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBox Text="{Binding EmailAddress}" Height="23" Margin="10,0,0,0" TextWrapping="Wrap" VerticalAlignment="Top" HorizontalAlignment="Stretch" Grid.Column="1"/>
<Label Content="Email Address:" Margin="10,0" VerticalAlignment="Top" FontWeight="Bold"/>
</Grid>
<Grid Height="26" Margin="10" VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="209"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBox Text="{Binding PhoneNumber}" Height="23" Margin="10,0,0,0" TextWrapping="Wrap" VerticalAlignment="Top" HorizontalAlignment="Stretch" Grid.Column="1"/>
<Label Content="Phone Number:" Margin="10,0" VerticalAlignment="Top" FontWeight="Bold"/>
</Grid>
<Grid Height="26" Margin="10" VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="209"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ComboBox Name="cbPreferredStore"
ItemsSource="{Binding Stores}" DisplayMemberPath="DisplayName" Height="23" Margin="10,0,0,0" VerticalAlignment="Top"
HorizontalAlignment="Stretch" Grid.Column="1" SelectedValue="{Binding ElementName=customerViewModel, Path=PreferredStoreId}">
<ComboBox.DataContext>
<localViewModels:StoreListViewModel />
</ComboBox.DataContext>
</ComboBox>
<Label Content="Preferred Store:" Margin="10,0" VerticalAlignment="Top" FontWeight="Bold"/>
</Grid>
<Grid Height="26" Margin="10" VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="209"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBox Text="{Binding Password}" Height="23" Margin="10,0,0,0" TextWrapping="Wrap" VerticalAlignment="Top" HorizontalAlignment="Stretch" Grid.Column="1"/>
<Label Content="Password:" Margin="10,0" VerticalAlignment="Top" Height="26" FontWeight="Bold"/>
</Grid>
</StackPanel>
StoreListViewModel代码:
public class StoreListViewModel : BaseViewModel
{
private List<StoreViewModel> _stores;
public List<StoreViewModel> Stores
{
get { return this._stores; }
set
{
this._stores = value;
notifyPropertyChanged("Stores");
}
}
public StoreListViewModel()
{
EasyDayTea.EasyDayTeaClient client = new EasyDayTea.EasyDayTeaClient();
_stores = client.GetStores(App.AppUserTeaCredental).Select(s => (StoreViewModel)s).ToList();
client.Close();
}
}
公共类StoreListViewModel:BaseViewModel
{
私家店铺名单;;
公共列表商店
{
获取{返回此。_存储;}
设置
{
这个。_存储=值;
通知财产变更(“存储”);
}
}
public StoreListViewModel()
{
EasyDayTea.EasyDayTeaClient=新的EasyDayTea.EasyDayTeaClient();
_stores=client.GetStores(App.appuserteacredent)。选择(s=>(StoreViewModel)s.ToList();
client.Close();
}
}
我认为CustomerServiceWModel中的PreferredStoreId
属性正确实现了接口
如果为true,则需要将组合框的SelectedValue更改为SelectedItem
,因为SelectedItem属性返回当前选定的整个对象。但是,属性和SelectedValue
一起用作SelectedItem属性的替代,据我所知,这不是您的选择
这里也有:
SelectedValue="{Binding ElementName=customerViewModel, Path=PreferredStoreId}"
不需要ElementName,因为CustomServiceWModel设置了默认分配的
DataContext
。您能显示存储
属性的实现吗?您是否实现了INotifyPropertyChanged?您是否尝试从绑定中删除ElementName=CustomServiceWModel?我已将StoreListViewModel和StoreViewModel添加到原始帖子中。BaseViewModel实现INotifyPropertyChanged
SelectedValue="{Binding ElementName=customerViewModel, Path=PreferredStoreId}"