XAML:访问用户控件内的控件



我有这样的用户控制:

<Grid x:Name="LayoutRoot" Background="Transparent">
    <Image Source="/Images/btn1_normal@2x.png" Stretch="Fill" />
    <TextBlock x:Name="Text" Text="Button" Foreground="Black"
               VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>

我在另一个 XAML 中使用此用户控件,如下所示:

<MyControlls:MyButton Width="90" Height="55"/>

现在,我如何在此 XAML 中访问名为文本的文本块并更改他的文本(在 Windows Phone 8 中)?像这样:

<MyControlls:MyButton Width="90" Height="55">
    <MyButton.Text Text="Hello World!" />        
</MyControlls:MyButton>`

谢谢。

我找到了解决方案。

<UserControl x:Class="Becel.Controls.MyButton"
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"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
x:Name="MyUserControll">
<Grid x:Name="LayoutRoot" Background="Transparent">
    <Image x:Name="myImage" Source="/Images/btn1.9_normal@2x.png" Stretch="Fill"
MouseEnter="Image_MouseEnter" MouseLeave="Image_MouseLeave" />
    <TextBlock x:Name="textTitle" Text="{Binding Title, ElementName=MyUserControll}" VerticalAlignment="Center" HorizontalAlignment="Center" TextAlignment="Center" Foreground="Black" FontSize="25"  MouseEnter="Image_MouseEnter" MouseLeave="Image_MouseLeave"/>
</Grid>
</UserControl>

在 C# 代码中:

public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof (String), typeof(MyButton),null);
    public String Title
    {
        get { return (String)GetValue(TitleProperty); }
        set { SetValue(TitleProperty, value); }
    }

然后在任何地方都可以像这样使用:

<MyUserControl:MyButton Width="90" Height="55" Margin="10 0 0 0" Title="Hello Wold!" />

你不能直接这样做。这里有两个选择。

解决方案 1:使用视图模型

使用视图模型类存储需要在自定义控件中显示的属性。

class MyButtonViewModel
{
    public string Text { get;set; }
}

MyButton.xaml:

<TextBlock Text={Binding Text} ...

MainWindow.xaml

<Window.Resources>
    <MyButtonViewModel x:Key="myButtonVm" Text="Hello!" />
</Window.Resources>
<MyButton DataContext={StaticResource myButtonVm} />

这使用 MVVM 模式。请参阅具有模型-视图-视图模型设计模式的 WPF 应用(如果从未使用过)。

解决方案 2:使用自定义控件

将您的UserControl替换为CustomControl。在这种情况下,Text可以是依赖项属性,以便您可以编写。

<MyButton Text="Hello !" />

这要复杂得多。请参阅如何创建 WPF 自定义控件。

选择哪个?

如果您打算在多个不同的项目中使用控件,并且需要能够更改控件的外观,请选择UserControl

否则,请选择ViewModel,并最终在项目的剩余部分应用 MVVM 模式。

要完成此任务,一种解决方案可能是将依赖属性声明到您的Control(似乎称为"MyButton")代码中:

  public string ButtonText
  {
    get { return (string )this.GetValue(ButtonTextProperty); }
    set { this.SetValue(ButtonTextProperty, value); } 
  }
  public static readonly DependencyProperty ButtonTextProperty = DependencyProperty.Register(
    "ButtonText", typeof(string), typeof(MyButton),new PropertyMetadata(false));

然后你必须绑定到你的 xaml 代码中:

  <YourControl x:Name="MyButtonName">
    ... // some xaml code
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Image Source="/Images/btn1_normal@2x.png" Stretch="Fill" />
        <TextBlock x:Name="Text" Text="Button" Foreground="Black"
                   VerticalAlignment="Center" HorizontalAlignment="Center"
                   Text="{Binding Path=ButtonText, ElementName=MyButtonName}"/>
    </Grid>
 </YourControl>

最后,您可以使用"我的按钮"Control

<MyControlls:MyButton Width="90" Height="55" ButtonText="Hello World!">
</MyControlls:MyButton>

最新更新