Windows phone 8 如何在listbox中绑定数据用户控件

Windows phone 8 如何在listbox中绑定数据用户控件,windows-phone-8,Windows Phone 8,我试图将用户控件绑定到listbox中,但似乎我做错了什么,尽管我遵循了,但这并没有帮助 这里是我在用户控制中所做的 <Grid x:Name="LayoutRoot" Background="Transparent" > <Grid.RowDefinitions> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <C

我试图将用户控件绑定到listbox中,但似乎我做错了什么,尽管我遵循了,但这并没有帮助

这里是我在用户控制中所做的

<Grid x:Name="LayoutRoot" Background="Transparent" >
<Grid.RowDefinitions>
  <RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
   <ColumnDefinition Width="Auto"></ColumnDefinition>
   <ColumnDefinition Width="*"></ColumnDefinition>
   <ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Image Source="Images/download.png" Width="30" Height="30" VerticalAlignment="Top" Grid.Column="0"/>
<ProgressBar Grid.Row="0" Grid.Column="1" x:Name="prg" VerticalAlignment="Top" />
<TextBlock x:Name="TextBlock" Foreground="Black" Text="{Binding Text}" Grid.Row="0" Grid.Column="2" VerticalAlignment="Top" FontSize="20" HorizontalAlignment="Left" />
</Grid>
public static readonly DependencyProperty TextProperty =
 DependencyProperty.Register(
    "Text",
    typeof(string),
    typeof(SuraWithProgressBar),
    new PropertyMetadata(null));

public string Text
{
  get { return (string)GetValue(TextProperty); }
  set { SetValue(TextProperty, value);}
}
用法

<ListBox x:Name="lsbQuranData" Grid.Row="1" Foreground="Black" ScrollViewer.VerticalScrollBarVisibility="Visible">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<local:SuraWithProgressBar Text="{Binding SuraTName, ElementName=SuraWithProgressBar}"></local:SuraWithProgressBar>
<Line X1="0" X2="480" Y1="0" Y2="0" VerticalAlignment="Bottom" StrokeThickness="2" Stroke="Black" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

让我提到“App.Chapter”包含“SuraTName”,它绑定到用户控件的文本属性。用户控件添加到列表框中,但文本不显示。

转换器对于调试绑定问题是一件好事=>

 public sealed class DebugConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        return value;
    }
}

最后,我提出了以下解决方案

public static readonly DependencyProperty TextProperty =
 DependencyProperty.Register(
    "Text",
    typeof(string),
    typeof(SuraWithProgressBar),
    new PropertyMetadata(null));

我把“文本”改成了“SuraTName”,它成功了

当你添加一个转换器时,你的值不是空的?很抱歉,我没有得到它,我没有使用转换器。(显示114个项目,绑定值从不为空)如果您写入任何内容而不是绑定。例如“托托”?什么是append?当我用“TextBlock.Text=value”编辑用户控件中的Text属性时,没有显示任何内容(它是空的);我只是在那里追加了这个语句,它显示了文本“toto”,但没有显示绑定。好的,我想我找到了,您在元素SuraWithProgressBar中搜索SuratName属性,这是错误的。删除此属性,只需编写text=“{binding SuratName}”。谢谢,在转换器中,我可以在调试时看到该值。它返回值,但textblock中不显示任何内容。
public static readonly DependencyProperty TextProperty =
 DependencyProperty.Register(
    "Text",
    typeof(string),
    typeof(SuraWithProgressBar),
    new PropertyMetadata(null));