簡體   English   中英

Windows Store應用程序的DisplayMemberPath =“ Value”的替代方案是什么?

[英]What is the alternative for DisplayMemberPath=“Value” for Windows Store applications?

我認為Windows Store應用程序在使用DisplayMemberPath =“ Value”時存在錯誤。

這是我的代碼

    <ComboBox Height="40" VerticalAlignment="Stretch" SelectedValuePath="Key" DisplayMemberPath="Value" x:Name="comboBox1" FontSize="25"/>

var source = new Dictionary<string, double>();
            source.Add("Item1", 0.4);
            source.Add("Item2", 0.3);
            source.Add("Item3", 0.1);
            source.Add("Item4", 0.1);

            var formateDSource = new Dictionary<string, string>();

            foreach (var item in source)
            {
                formateDSource.Add(string.Format("[{0}, {1}]", item.Key, item.Value), item.Key);
            }

            comboBox1.ItemsSource = source;

如果在WPF中使用此代碼,效果會很好。 但是,如果您在Windows Store應用程序中使用此代碼,則組合框為空,並引發錯誤。 那么,在Windows Store應用程序中是否有其他方法可以做到這一點,而我是否發現了一個錯誤? 因為我已經研究Web數天,卻沒有找到解決方案。*除非您在Visual Studios中將我的代碼作為Windows Store應用而非WPF嘗試過,否則請不要評論。

我已經能夠在通用應用程序(Windows Phone版本)中重現它。

若要解決此問題,請首先刪除組合框上的“高度”屬性值,因為這將阻止組合框在打開組合框時向您顯示選項(至少在Windows Phone上)。

然后,在后面的代碼中,嘗試將Dictionnary轉換為對象列表(或其他Dictionnary)(我使用了匿名對象,但最好創建自定義Type):

comboBox1.ItemsSource = formateDSource.Select(f => new { Key = f.Key, Value = f.Value }).ToList();

所以我的xaml看起來像這樣:

<ComboBox x:Name="comboBox1"
              VerticalAlignment="Stretch"
              DisplayMemberPath="Value"
              FontSize="25"
              SelectedValuePath="Key" />

現在,我還不太了解原始詞典的內容,但是至少您現在應該有一種解決方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM