【问题标题】:How to bind dictionary key with the listbox textblock?如何将字典键与列表框文本块绑定?
【发布时间】:2014-07-19 01:29:47
【问题描述】:

在 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 并显示键值对,但无法将键与文本块绑定。 请告诉我解决方案....

【问题讨论】:

  • 代码应该可以工作。你能检查listview的父级吗?可能是容器被隐藏或其宽度/高度设置为 0!你能分享整个页面(xaml)代码

标签: c# windows windows-phone-8 windows-phone


【解决方案1】:

你可以做到显示here(用wpf在列表框中绑定字典的键和值)

【讨论】:

    猜你喜欢
    • 2013-07-13
    • 2011-11-04
    • 2016-08-28
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 2013-08-20
    • 1970-01-01
    • 2012-04-07
    相关资源
    最近更新 更多