Click here to Skip to main content
16,022,538 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a problem with a combobox in a windows store app with a propably simple solution:)
XML
...
<DataTemplate x:Key="Schablone">
    <ContentPresenter Content="{Binding FElement}" />
</DataTemplate>
...
<ComboBox ItemsSource="{Binding Path=AuswahlListe}" 
            SelectedValue="{Binding Path=Id, Mode=TwoWay}" 
            SelectedValuePath="Id"
            ItemTemplate="{StaticResource Schablone}"
            Width="200" HorizontalAlignment="Left"
/>
...

When I use the arrow keys of the keyboard, everything works fine:
Screenshot arrow keys
When I open the combobox with function key F4 or use the mouse to select an item, the item is correct selected but the combobox don't show a value:
Open combobox
Empty combobox

If I use DisplayMemberPath instead of DataTemplate everything works fine:
XML
<ComboBox ItemsSource="{Binding Path=AuswahlListe}" 
            SelectedValue="{Binding Path=Id, Mode=TwoWay}" 
            SelectedValuePath="Id"
            DisplayMemberPath="Id"                
            Width="200" HorizontalAlignment="Left"
/>


The sample solution you can find here.

Greetings from Germany
Ralf
Posted
Updated 11-Jan-15 2:28am
v2

1 solution

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:
C#
public void AListeFüllen()
{
    // Im tatsächlichen Projekt sind diese Strukturen umfangreicher.
    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
C#
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
C#
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:
XML
<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
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900