有没有人有将 2 个相关的 ComboBoxs 绑定到 DataGrid 的选定项的示例?我试过了,但我似乎无法让它工作。我对 MVVM 相当陌生,所以我做错了。
NewClient
我有一个数据网格,它绑定到我的 ViewModel
中命名的对象中的集合ClientMarketCodes
。
ClientMarketCodes
有一个名为的导航属性MarketCodeTypes
,它有一个市场导航属性。所以我想在数据网格中显示代码MarketCodeTyeName
,,MarketName
。我已设法在 DataGrid 组合框中显示Markets
和MarketCodeTypes
,但我想在用户只能选择Markets
与那里相关的地方之间建立关系。我已向Combobox 添加了一个选定项目,在更改它时执行绑定到 Combobox 的集合的填充方法,但由于过滤器适用于Combobox 列的所有行,因此它无法正常工作。我在两个组合框中都发生了一些事情,例如更改MaketCodeTypes
MarketCodeTypes
Market
Markets
MarketCodeTypes
MarketCodeTypes
MarketCodetypes
Markets
一行的组合框显示所有其他市场我选择了另一个市场并单击该行不受影响并返回其原始市场。我不知道为什么会发生这种情况。
这是我的视图模型:
public class MarketsViewModel : ObservableObject, IMarketsViewModel, INavigationAware, IConfirmNavigationRequest, IRegionMemberLifetime
{
#region MarketsViewModel
public MarketsViewModel()
{
//NewClient.ClientMarketCodes[0].MarketCodeType.MarketID
//MarketCodeTypes = new ObservableCollection<MarketCodeType>();
this.GetMarkets();
//this.GetMarketCodeTypes();
this.PropertyChanged += MarketssViewModel_PropertyChanged;
}
private void MarketssViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case "selectedMarket":
{
if (selectedMarket != null)
{
GetMarketCodeTypes();
}
break;
}
}
}
#endregion
#region --Members--
private ObservableCollection<Market> m_Market;
private ObservableCollection<MarketCodeType> m_MarketCodeType;
private List<MarketCodeType> m_MarketCodeTypes;
public List<MarketCodeType> MarketCodeTypesList
{
get
{
if (m_MarketCodeTypes == null)
{
m_MarketCodeTypes = new List<MarketCodeType>();
}
return m_MarketCodeTypes;
}
set
{
m_MarketCodeTypes = value;
RaisePropertyChanged("MarketCodeTypesList");
RaisePropertyChanged("MarketCodeTypes");
}
}
public ObservableCollection<MarketCodeType> MarketCodeTypes
{
get
{
if (MarketCodeTypesList != null)
return new ObservableCollection<MarketCodeType>(MarketCodeTypesList);
else
return new ObservableCollection<MarketCodeType>();
}
}
#endregion
#region --Properties--
public ObservableCollection<Market> Markets
{
get { return m_Market; }
set
{
m_Market = value;
RaisePropertyChanged("Markets");
}
}
private Market _selectedMarket;
public Market selectedMarket
{
get { return _selectedMarket; }
set
{
if (_selectedMarket != value)
{
_selectedMarket = value;
RaisePropertyChanged("selectedMarket");
RaisePropertyChanged("MarketCodeTypes");
}
}
}
#endregion
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);
}
}
private void GetMarketCodeTypes()
{
try
{
MarketCodeType[] MarketCodeTypeArr;
using (var client = new ClientServiceProxy())
{
MarketCodeTypeArr = client.GetAllMarketCodeTypes().Where(c=>c.MarketID==selectedMarket.marketID).ToArray();
}
if (MarketCodeTypeArr != null)
{
MarketCodeTypesList = MarketCodeTypeArr.ToList();
}
else
{ MarketCodeTypesList = new List<MarketCodeType>(); }
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
}`
这是我的 Xaml:
<Grid x:Name="grid2" RenderTransformOrigin="0.5,0.5">
<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="???? ?????"/>-->
<Custom:DataGridTextColumn Header="Code" Binding="{Binding Code,Mode=TwoWay}" IsReadOnly="False" />
<Custom:DataGridTemplateColumn Header="MarketCodeType" >
<Custom:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding MarketCodeType.NameA}"/>
</DataTemplate>
</Custom:DataGridTemplateColumn.CellTemplate>
<Custom:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox Name="cmbMarketCodeTypes"
ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType=my:MarketsView},Path=DataContext.MarketCodeTypes}"
DisplayMemberPath="NameA"
SelectedValue="{Binding Path=MarketCodeTypeID,Mode=TwoWay,ValidatesOnDataErrors=True}"
/>
</DataTemplate>
</Custom:DataGridTemplateColumn.CellEditingTemplate>
</Custom:DataGridTemplateColumn>
<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>