我想问一下,因为我几个小时都在努力解决这个问题。
我实现了一个数据网格,并希望填充"春天的高度" (TextBox)基于所选的" SP Type" (ComboBox)在同一行内。
值已填充,但未显示,我必须点击“#34;春天的高度"单元格,然后显示一个值。我已经尝试过UpdateSourceTrigger = LostFocus,而不是UpdateSourceTrigger = PropertyChanged,没有用。
SelectedPbType属性包括用于检索所需值的函数,该值应在选择SP Type后立即显示。
很抱歉命名不匹配。 SP类型表示PB类型。
请您看一下,告诉我我的代码有什么问题?谢谢。
查看:
<dg:DataGrid Height="330" HorizontalAlignment="Stretch" Margin="5,5,0,0"
x:Name="autocadCoordinationsDataGrid" VerticalAlignment="Top" RowHeight="25" ColumnWidth="Auto"
HeadersVisibility="Column"
Background="#e6ecff"
BorderBrush="Gray"
BorderThickness="2"
SelectionMode="Single"
AutoGenerateColumns="False"
IsSynchronizedWithCurrentItem="False"
CanUserAddRows="false"
CanUserDeleteRows="False"
CanUserReorderColumns="False"
ItemsSource="{Binding CadCoordinates}"
SelectedItem="{Binding SelectedCadCoordinate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<dg:DataGridTemplateColumn x:Name="cbTempPbType" Header="{lex:Loc EnvironmentCoordinatesGridColumnPbType}" Width="SizeToHeader" >
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="cbPbType" ItemsSource="{Binding Source={StaticResource PbTypes}}"
SelectedItem="{Binding SelectedPbType, Mode=TwoWay, UpdateSourceTrigger=LostFocus, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}"
DisplayMemberPath="Name"
IsSynchronizedWithCurrentItem="False"
SelectedValue="{Binding SelectedPbType.Index}"
SelectedValuePath="Index">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding SelectionPbTypeChangedCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>
<dg:DataGridTextColumn
Header="{lex:Loc EnvironmentCoordinatesGridColumnPillarsHeight}"
Width="SizeToHeader"
EditingElementStyle="{StaticResource CellEditStyle}"
Binding="{Binding PillarsHeight,UpdateSourceTrigger=LostFocus, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, Mode=TwoWay, ValidatesOnExceptions=True}" >
<dg:DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="TextWrapping" Value="Wrap"/>
</Style>
</dg:DataGridTextColumn.ElementStyle>
</dg:DataGridTextColumn>
</dg:DataGrid.Columns>
</dg:DataGrid>
型号:
public class CadCoordinatesGrid : ViewModelBase, INotifyPropertyChanged
{
public event EventHandler Changed;
private EnvironmentViewModel _viewModel;
public CadCoordinatesGrid()
{
}
public CadCoordinatesGrid(EnvironmentViewModel viewModel)
{
_viewModel = viewModel;
}
private ObservableCollection<PbType> _pbType;
public ObservableCollection<PbType> PbType
{
get
{
return _pbType;
}
set
{
_pbType = value;
OnPropertyChanged("PbType");
}
}
private PbType _selectedPbType;
public PbType SelectedPbType
{
get
{
return _selectedPbType;
}
set
{
if (_viewModel != null) _viewModel.IsDirty = true;
this.PillarsHeight = EnvironmentHelper.GetPillarHeightFrom(value);
//OnPropertyChanged("PillarsHeight");
_selectedPbType = value;
OnPropertyChanged("SelectedPbType");
}
}
private string _pillarsHeight;
public string PillarsHeight
{
get
{
return _pillarsHeight;
}
set
{
CommonHelper.TryParseDecimal(value);
if (_viewModel != null) _viewModel.IsDirty = true;
_pillarsHeight = value;
OnPropertyChanged(() => PillarsHeight);
}
}
private ICommand selectionPbTypeChangedCommand;
public ICommand SelectionPbTypeChangedCommand
{
get
{
if (selectionPbTypeChangedCommand == null)
{
selectionPbTypeChangedCommand = new RelayCommand(param => this.PbTypeChangeSelected(),
null);
}
return selectionPbTypeChangedCommand;
}
}
/// <summary>
/// Changes the selected.
/// </summary>
private void PbTypeChangeSelected()
{
OnPropertyChanged(()=> this.PillarsHeight);
}
/// <summary>
/// Called when [property changed].
/// </summary>
/// <param name="propertyName">Name of the property.</param>
protected override void OnPropertyChanged(string propertyName)
{
var hander = this.Changed;
if (hander != null)
{
hander(this, EventArgs.Empty);
}
}
}
答案 0 :(得分:0)
它是设置SelectedPbType
属性的PillarsHeight
属性的setter。
摆脱交互触发器和ComboBox的SelectedValue绑定和SelectedValuePath:
<ComboBox x:Name="cbPbType" ItemsSource="{Binding Source={StaticResource PbTypes}}"
SelectedItem="{Binding SelectedPbType, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}"
DisplayMemberPath="Name">
</ComboBox>
您不应该同时绑定ComboBox的SelectedItem和SelectedValue属性。