C# 如何将字典键与listbox文本块绑定?

C# 如何将字典键与listbox文本块绑定?,c#,windows,windows-phone-8,windows-phone,C#,Windows,Windows Phone 8,Windows Phone,在ListView的DataTemplate中进行绑定时遇到问题。我的绑定目标是KeyValuePair 这是我的XAML代码:- <ListBox x:Name="ListBox_Setting" ItemsSource="{Binding DictTemperature}" BorderBrush="Black" BorderThickness="1" Height="582" Width="300"> <ListBox.ItemTemplate&

在ListView的DataTemplate中进行绑定时遇到问题。我的绑定目标是KeyValuePair

这是我的XAML代码:-

 <ListBox x:Name="ListBox_Setting" ItemsSource="{Binding DictTemperature}" BorderBrush="Black" BorderThickness="1" Height="582" Width="300">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Height="78">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Image x:Name="Img_ListSetting" Width="50" Height="50" Source="/Assets/un-checked.png" Margin="18,7,507,21" />
                        <TextBlock x:Name="Tb_ListSetting" Text="{Binding Path=Key}" FontFamily="OpenSans" FontSize="30" Margin="89,0,18,20" Height="42" VerticalAlignment="Bottom" />
                        <Rectangle x:Name="Line_ListSetting" Height="1" Margin="15,66,15,11" Fill="#FF3498DB"/>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

这是我的C代码:-

公共字典{set;get;}
DictTemperature=新字典();
如果(!appSettings.Contains(“AppTemperature”))
{
记录温度。添加(“摄氏度”,真);
记录温度。添加(“华氏”,真);
记录温度。添加(“开尔文”,真);
DictTemperature.Add(“Rankine”,true);
appSettings.Add(“AppTemperature”,DictTemperature);
}
DictTemperature=(字典)appSettings[“AppTemperature”];
这里listbox将count显示为4,并显示键值对,但无法将键与textblock绑定。
请告诉我解决方案……

您可以按所示执行(使用wpf在列表框中绑定词典的键和值)

代码应该可以工作。您可以检查listview的父级吗?可能是容器隐藏或其宽度/高度设置为0!您可以共享整个页面(xaml)代码吗
public Dictionary<string, bool> DictTemperature { set; get; }
DictTemperature = new Dictionary<string, bool>();


        if (!appSettings.Contains("AppTemperature"))
        {
            DictTemperature.Add("Celsius", true);
            DictTemperature.Add("Fahrenheit", true);
            DictTemperature.Add("Kelvin", true);
            DictTemperature.Add("Rankine", true);
            appSettings.Add("AppTemperature", DictTemperature);


        }
        DictTemperature = (Dictionary<string, bool>)appSettings["AppTemperature"];