私はこの問題を何時間も苦労しているので、お聞きしたいと思います。WPFデータグリッド - セルが他のセルの値に基づいて更新される
データグリッドを実装し、同じ行内の選択された「SPタイプ」(ComboBox)に基づいて「スプリングポイントの高さ」(TextBox)を設定したいとします。
値が示さ取り込まれていないが、私は「春のポイントの高さ」とは、細胞だけにして値が示されているをクリックする必要があります。私はすでにUpdateSourceTrigger = LostFocus isteadの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);
}
}
}
返信ありがとうございますが、役に立たなかったです。そしてあなたは正しい、SelectedPbTypeはPillarsHeightプロパティを設定します。 – Lukas
おそらく最小限で再現性のあるサンプルを提供できますか?http://stackoverflow.com/help/mcve不必要な、関連性のないすべての基本クラス、型、静的リソースなどを取り除きます。DataGridTextColumnがバインドされているソースプロパティを設定すると機能するはずです。 – mm8
これはもう必要ではありません。今は動作し、コードを抜けて不要なコードを削除しました。あなたは私にアイディアをくれました、ありがとう – Lukas