The Standard way to solve a Problem is often the fastest and best way:
"Reduce your Problem until it disapears":)
After some thoughts about the possible Combobox process I suspected the ContentPresenter.
I assume that, if the selection is made from the open list, the Combobox needs a copy of the selected item.
Next assumption: The Combobox copied only the DataTemplate, not the binded value.
And here is the problem:
The binded value is a framework element object (Grid or ComboboxItem, I tried both) and a framework element object can only be content or child of one framework parent element:
public void AListeFüllen()
{
ComboBoxItem cbi1 = new ComboBoxItem();
Grid gr1 = new Grid();
TextBlock ta1 = new TextBlock();
ta1.Text = "Wert 1";
gr1.Children.Add(ta1);
cbi1.Content = gr1;
AuswahlListe.Add(new Element(1, cbi1));
...
}
And while the combobox didn't made a copy of the binded value my binded value was content of two parents: the shown selected value and the combobox-list.
After I Change the design from
public class Element : INotifyPropertyChanged
{
internal void RaisePropertyChanged(string prop)
{
if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(prop)); }
}
public event PropertyChangedEventHandler PropertyChanged;
private int _Id;
public int Id
{
...
}
public FrameworkElement FElement { get; set; }
public Element(int id, FrameworkElement felement)
{
FElement = felement;
Id = id;
}
public Element()
{
}
}
to
public class Element : INotifyPropertyChanged
{
internal void RaisePropertyChanged(string prop)
{
if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(prop)); }
}
public event PropertyChangedEventHandler PropertyChanged;
private int _Id;
public int Id
{
...
}
public Visibility ASichtbar { get; set; }
public Visibility BSichtbar { get; set; }
public Visibility CSichtbar { get; set; }
public string A { get; set; }
public string B { get; set; }
public string C { get; set; }
public Element(int id, Visibility asichtbar, string a, Visibility bsichtbar, string b, Visibility csichtbar, string c)
{
ASichtbar = asichtbar;
BSichtbar = bsichtbar;
CSichtbar = csichtbar;
A = a;
B = b;
C = c;
Id = id;
}
public Element()
{
}
}
So I can use this values in the DataTemplate:
<datatemplate x:key="Schablone" xmlns:x="#unknown">
<grid>
<textblock foreground="Red" visibility="{Binding ASichtbar}" text="{Binding A}" />
<textblock foreground="Orange" visibility="{Binding BSichtbar}" text="{Binding B}" />
<textblock foreground="Green" visibility="{Binding CSichtbar}" text="{Binding C}" />
</grid>
</datatemplate>
And now it works:):
Solution, solved
I hope, that this will prevent other to make this "mistake".
Greetings from Germany
Ralf