這一個不工作的組合框讓我發瘋了...數據綁定DataGrid中
這裏是我使用綁定的組合框是在一個DataGrid列中的XAML。 ItemSource是持有類「Pipetter」的ObservableCollection。 CellTemplate只需顯示當前選定行的此Pipetter類的「name」屬性。
問題是,只要我在組合框中選擇一個值,所選的值就突然出現在該列的所有行中。我重新設計了許多不同的方式,並且在每種情況下都發生了。關於什麼設置關閉的任何想法?
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox
IsEditable="False"
ItemsSource="{Binding DataContext.Pipettors, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Mode=TwoWay}"
SelectedItem="{Binding DataContext.SelectedPipettor, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedValue="{Binding TipGroup.PipettorTipType.Pipettor}"
DisplayMemberPath="Name"
/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label
Content="{Binding TipGroup.PipettorTipType.Pipettor.Name}"
Style="{DynamicResource DataGridRowLabel}"
/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
這是selectedItem綁定到的Vm中的屬性。我只是將「SelectedItem」並將其分配給當前選定行(SelectedTipGroup)中的相應屬性。這被定義爲DataGrid定義中的「SelectedItem」。
private Pipettor selectedPipettor;
public Pipettor SelectedPipettor
{
get { return selectedPipettor; }
set
{
SetProperty(ref selectedPipettor, value);
SelectedTipGroup.TipGroup.PipettorTipType.Pipettor = value;
}
}
我更新的組合框結合的建議:
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox x:Name="PipetterComboBox"
ItemsSource= "{Binding DataContext.Pipettors, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Mode=TwoWay}"
SelectedItem="{Binding TipGroup.PipettorTipType.Pipettor}" IsSynchronizedWithCurrentItem="True"
DisplayMemberPath="Name"
/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding TipGroup.PipettorTipType.Pipettor.Name}" Style="{DynamicResource DataGridRowLabel}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
而且仍然在進行選擇時在DataGrid中的一個行所作,同樣的值將出現在所有行該列......這是隻是想給將selectedItem類分配給當前行中的類屬性「移液器」 ......
曾花費數天時間在這一個..沒有任何意義......
謝謝!
這是組合框綁定到的屬性。組合框的ItemsSource只是類型移液管的可觀察集合。
private Pipettor pipettor;
[DataMember]
public Pipettor Pipettor
{
get { return this.pipettor; }
set
{
if (SetProperty(ref this.pipettor, value))
{
//***BKM This was failing on delete - not sure if masking or not but will null check
//note something similar in other models - review
if (value != null)
{
this.pipettorId = this.pipettor.Identity;
}
}
}
}
和setProperty()
protected bool SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
{
if (object.Equals(field, value))
{
return false;
}
field = value;
//Check to make sure Tracking is Initialized but no need to do anything
if (!this.Tracking.Initialized)
{
}
RaisePropertyChanged(propertyName);
return true;
}
也許在你的財產的二傳手通知IPropertyChanged? – ProgrammingDude
SetProperty是做什麼的? PropertyChanged在那裏解僱了嗎? –
setproperty只是封裝檢查以查看新值是否爲!=當前值,如果是則引發propertyChanged事件並將後臺字段設置爲新值。 –