亲爱的,我在数据网格中有一个组合框列,绑定到我的 ViewModel 中的可观察集合市场,而数据网格绑定到集合 ClientMarketCode,它是 NewClient 对象中的集合。ClientMarketCode 具有此属性 Code、MarketCodeTypeID、MarketID。所以我使用绑定到市场的组合框以防用户想要编辑市场它显示所有市场并且用户选择他想要的市场。我在选择任何其他市场时遇到问题,因为所选行中的市场值在选择后没有改变来自组合框的新市场。
例如:
假设第一行有 KSA 市场,然后我在单击 USA 后将其从组合框更改为 USA,它将 baco 返回到 KSA,我不知道 y。
这是我的视图模型:
public class MarketsViewModel : ObservableObject, IMarketsViewModel, INavigationAware, IConfirmNavigationRequest, IRegionMemberLifetime
{
#region MarketsViewModel
public MarketsViewModel()
{
this.GetMarkets();
}
public ObservableCollection<Market> Markets
{
get { return m_Market; }
set
{
m_Market = value;
RaisePropertyChanged("Markets");
}
}
private void GetMarkets()
{
try
{
Market[] MarketArr;
using (var client = new ClientServiceProxy())
{
MarketArr = client.GetAllMarkets();
}
if (MarketArr != null)
{
this.Markets = new ObservableCollection<Market>(MarketArr);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
}
这是 XAML:
<Custom:C1DataGrid x:Name="c1DataGrid"
AutoGenerateColumns="False"
Height="490" ItemsSource="{Binding Path=NewClient.ClientMarketCodes,Mode=TwoWay,UpdateSourceTrigger=LostFocus,ValidatesOnDataErrors=True,ValidatesOnExceptions=True}">
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFBEBE00" Offset="0.5"/>
<GradientStop Color="#FFEDED9A" Offset="0.496"/>
</LinearGradientBrush>
</Custom:C1DataGrid.HeaderBackground>
<Custom:C1DataGrid.Columns>
<!--<Custom:DataGridCheckBoxColumn Header="Code"/>-->
<Custom:DataGridTemplateColumn Header="Markets">
<Custom:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding MarketCodeType.Market.NameA}"/>
</DataTemplate>
</Custom:DataGridTemplateColumn.CellTemplate>
<Custom:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox Name="cmbMarkets" ItemsSource= "{Binding RelativeSource={RelativeSource FindAncestor,AncestorType=my:MarketsView},Path=DataContext.Markets,Mode=TwoWay}"
DisplayMemberPath="NameA" SelectedValue="{Binding Path=MarketCodeType.Market.MarketID,Mode=TwoWay}"
SelectedValuePath="MarketCodeType.MarketID"
SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType=my:MarketsView},Path=DataContext.selectedMarket,Mode=TwoWay}" />
</DataTemplate>
</Custom:DataGridTemplateColumn.CellEditingTemplate>
</Custom:DataGridTemplateColumn>
</Custom:DataGridTemplateColumn>
</Custom:C1DataGrid.Columns>
</Custom:C1DataGrid>