0
我創建了一個用戶控件WPF:如何在窗口電話中將參數傳遞給usercontrol?
<UserControl
x:Class="BARCIndia.Mobile.Views.UserControls.SubCategoryUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:BARCIndia.Mobile.Views.UserControls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid>
<StackPanel Background="DarkCyan">
<TextBlock Name="txtName" FontSize="26"></TextBlock>
</StackPanel>
</Grid>
</UserControl>
的代碼背後有一個叫財產「ParamValue
」它設置爲我的用戶控制TextBlock:txtName
public sealed partial class SubCategoryUserControl : UserControl
{
public string ParamValue
{
get { return (string)GetValue(ParamValueProperty); }
set { SetValue(ParamValueProperty, value); }
}
public static readonly DependencyProperty ParamValueProperty = DependencyProperty.Register
(
"ParamValue",
typeof(string),
typeof(SubCategoryUserControl),
new PropertyMetadata(string.Empty)
);
public SubCategoryUserControl()
{
this.InitializeComponent();
txtName.Text = this.ParamValue;
}
}
的文本從XAML調用形式
<Grid >
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Myusercontrol:SubCategoryUserControl x:Name="tempUserControl" ParamValue="From Window" Grid.Row="0"/>
</Grid>
這裏我有通參數ParamValue = 「從窗口」當usercontrol加載的文本塊顯示爲空時。
屬性沒有得到更新,同時創造扶養財產
public static readonly DependencyProperty ParamValueProperty = DependencyProperty.Register
(
"ParamValue",
typeof(string),
typeof(SubCategoryUserControl),
**new PropertyMetadata(string.Empty)**
);
我得到如何分配的值賦予它的表現emtry串即默認值?
謝謝,但它不工作。我得到的解決方案我已經在Loaded方法中分配了textvalue。 private void UserControl_Loaded(object sender,RoutedEventArgs e) txtName.Text = ParamValue.ToString(); } –