使用嵌套成员将文本块绑定在列表框中

时间:2014-01-24 05:27:45

标签: c# windows-phone-8

我试图获取嵌套的类属性值并在列表框项中显示它。但似乎我的做法可能是错误的。

这是Listbox的XAML

<ListBox x:Name="lsbQuranData" Grid.Row="2" Foreground="Black" ScrollViewer.VerticalScrollBarVisibility="Visible" Style="{StaticResource ListBoxStyle1}" >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Width="480">
                <local:SuraWithProgressBar Width="480" SuraNumber ="{Binding Chapter.SuraNumber, Mode=TwoWay}"
                SuraName="{Binding Chapter.SuraTName}" Available="{Binding Available, Mode=TwoWay}"  />
                <Line X1="0" X2="480" Y1="0" Y2="0" VerticalAlignment="Bottom" StrokeThickness="2" Stroke="Black" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

课程

public class Chapter
{
    private string _suraName, _type, _ename, _tname;
    byte _suraNumber, _ruku;
    Int16 _order, _start, _ayas;

    public string SuraTName
    {
        get { return _tname; }
        set { _tname = value; }
    }

    public Byte SuraNumber
    {
        get { return _suraNumber; }
        set { _suraNumber = value; }
    }
}

public class ChapterSetting
{
    Chapter _chapter;
    string _available;

    public Chapter Chapter
    {
        get { return _chapter; }
        set { _chapter = value; }
    }

    public string Available
    {
        get { return _available; }
        set { _available = value; }
    }
}

列表框的数据绑定

lsbQuranData.ItemsSource = ds.getQuranSuraSetting();

方法&#34; getQuranSuraSetting&#34;返回我需要的所有值,但它不能绑定两个字段(SuraName,SuraNumber)。

是否可以将嵌套类成员与列表框项绑定?

谢谢!

1 个答案:

答案 0 :(得分:0)

您没有显示从 ds.getQuranSuraSetting()返回的内容。如果是IEnumerable<ChapterSetting>.

然后XAML应该可以工作。

<强> CODE

<StackPanel Width="480">
    <local:SuraWithProgressBar Width="480" 
                               SuraNumber ="{Binding Chapter.SuraNumber, Mode=TwoWay}"
                               SuraName="{Binding Chapter.SuraTName}" Available="{Binding Available, Mode=TwoWay}"  />
   <Line X1="0" X2="480" Y1="0" Y2="0" 
         VerticalAlignment="Bottom" StrokeThickness="2" 
         Stroke="Black" />
 </StackPanel>

您确实为我们提供了自定义控件的代码(SuraWithProgressBar),因此很难判断您的SuraNumber和SuraName属性是否有任何错误。

此外,如果您在SuraWithProgressBar控件中使用TextBlocks,请确认其前景属性未设置为与页面背景相同的颜色,否则您将看不到绑定数据