如何将字典键与列表框文本块绑定?

时间:2014-05-29 13:55:01

标签: 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>
                <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#代码: -

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"];

此处列表框显示计数为4并显示键值对,但无法将键绑定到文本块。 请告诉我解决方案......

1 个答案:

答案 0 :(得分:0)

你可以这样做显示here(使用wpf将字典&#39; s键和值放在列表框中)