簡體   English   中英

XAML:訪問用戶控件內的控件

[英]XAML: access to controls inside user control

我有這樣的userControl:

<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中使用此userControl,如下所示:

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

現在我如何在這個XAML中訪問名為Text的textBlock並更改他的文本(在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:使用ViewModel

使用視圖模型類來存儲需要在自定義控件中顯示的屬性。

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模式。 如果您從未使用過,請參閱使用Model-View-ViewModel設計模式的WPF應用程序

解決方案2:使用CustomControl

CustomControl替換UserControl 在這種情況下, 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>

最后,您可以使用“MyButton” Control

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM