如何在wpf usercontrol中将数据绑定属性设置为textblock?

时间:2016-11-17 18:21:29

标签: wpf data-binding

mFirebaseAdapter = new MessageAdapter(FriendlyMessage.class,
       R.layout.message_my,
       RecyclerView.ViewHolder.class,
       mFirebaseDatabaseReference.child("/messages"));

这是我的XAML文本区块代码,它是usercontrol UI元素的一部分。

我如何数据绑定到这个?

当我试图把

 <TextBlock Margin="0,109,20,0" VerticalAlignment="Top" Height="Auto" TextWrapping="Wrap" FontWeight="Bold"
            FontSize="14" HorizontalAlignment="Right" Foreground="#FF575757" RenderTransformOrigin="-3.611,3.696" ClipToBounds="True" Text="---------------------" Width="213"/>

XAML给出错误和设计视图变得不可见。

请帮助。

1 个答案:

答案 0 :(得分:1)

我想你想要

UserControl示例

    <UserControl x:Class="WpfApplication1.myusercontrol"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApplication1"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <TextBlock Name="ClickCount1" Text="12" />
    </Grid>
</UserControl>

使用带绑定的用户控件的窗口示例

    <Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <local:myusercontrol x:Name="MyUserControl" />
        <TextBlock Grid.Row="1" Text="{Binding ElementName=MyUserControl.ClickCount1, Path=Text}" />
    </Grid>
</Window>