2014-01-21 55 views
0

我想绑定列表框中的用户控件,但它似乎我做错了什么,虽然我遵循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”包含绑定到用户控件的Text属性的“SuraTName”。用户控件添加到列表框中,但不显示文本。

+0

当您添加转换器时,您的值不为空? – MatDev8

+0

对不起,我没有得到它,我没有使用转换器。 (显示114个项目并且绑定值永远不为空) – ARH

+0

如果您编写任何内容而不是绑定。例如“toto”?什么是追加? – MatDev8

回答

0

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

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

我将“文本”更改为“SuraTName”,它的工作原理!

0

转换器是用于调试结合问题是好事=>

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; 
    } 
} 
+0

谢谢,在转换器中,我可以在调试时看到值。它返回值,但没有任何内容显示在文本块中。 – ARH

相关问题