如何在列表框中绑定数据用户控件

时间:2014-01-21 15:51:12

标签: windows-phone-8

我正在尝试将列表框中的用户控件绑定,但看起来我做错了,虽然我跟着How to bind data user control inside listbox WP8但这无济于事。

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

<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>

以及用法背后的代码

lsbQuranData.ItemsSource = App.Chapter;

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

2 个答案:

答案 0 :(得分:0)

转换器对于调试绑定问题=&gt;

是一件好事
 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;
    }
}

答案 1 :(得分:0)

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

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

我将“文字”改为“SuraTName”并且有效!!