Store App Combobox DataTemplate问题 [英] Store App Combobox DataTemplate Problem

查看:74
本文介绍了Store App Combobox DataTemplate问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows商店应用程序中使用一个组合框有一个问题,它有一个可行的简单解决方案:)

I have a problem with a combobox in a windows store app with a propably simple solution:)

...
<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"

/>
...



当我使用键盘的方向键时,一切正常:

屏幕截图箭头键

当我用功能键F4打开组合框或使用鼠标选择项目,项目选择正确,但组合框没有显示值:

打开组合框

清空组合框



如果我使用DisplayMemberPath而不是DataTemplate一切正常:


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:

<ComboBox ItemsSource="{Binding Path=AuswahlListe}" 

            SelectedValue="{Binding Path=Id, Mode=TwoWay}" 

            SelectedValuePath="Id"

            DisplayMemberPath="Id"                

            Width="200" HorizontalAlignment="Left"

/>





您可以找到的示例解决方案其他



来自德国的问候

Ralf



The sample solution you can find here.

Greetings from Germany
Ralf

推荐答案

解决问题的标准方法通常是最快最好的方法:



减少你的问题,直到它消失:)



在对可能的Combobox进程有些想法后,我怀疑是ContentPresenter。



我假设,如果从打开的列表中选择,Combobox需要所选项目的副本。

下一个假设:Combobox仅复制了DataTemplate,而不是绑定值。

这就是问题所在:



绑定值是一个框架元素对象(Grid或ComboboxItem,我尝试过两种方法)框架元素对象只能是一个框架父元素的内容或子元素:

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()
{
    // 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

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()
    {
    }
}

< br $>


to



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()
    {
    }
}



所以我可以在DataTemplate中使用这个值:


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>



现在它可以工作:):

解决方案,解决了

我希望,这会阻止其他人犯这个错误。



来自德国的问候

Ralf


And now it works:):
Solution, solved
I hope, that this will prevent other to make this "mistake".

Greetings from Germany
Ralf


这篇关于Store App Combobox DataTemplate问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆