【问题标题】:How do you pass parameters to usercontrol in window phone?你如何将参数传递给窗口电话中的用户控件?
【发布时间】:2016-02-03 06:25:28
【问题描述】:

我在 WPF 中创建了一个用户控件:

<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="From Window"。

属性未更新其显示的 emtry 字符串,即创建依赖属性时分配的默认值

public static readonly DependencyProperty ParamValueProperty = DependencyProperty.Register
           (
                "ParamValue",
                typeof(string),
                typeof(SubCategoryUserControl),
                **new PropertyMetadata(string.Empty)**
           );

我如何获得赋值?

【问题讨论】:

    标签: wpf xaml user-controls windows-phone-8.1


    【解决方案1】:

    在您的情况下,它可能如下所示:

    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();
        }
    }
    

    还有xaml:

    <UserControl x:Class="BARCIndia.Mobile.Views.UserControls.SubCategoryUserControl"
                 x:Name="Root"
                 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 Text="{Binding ParamValue, ElementName=Root}" FontSize="26" />
        </StackPanel>
      </Grid>        
    </UserControl>
    

    【讨论】:

    • 谢谢,但它不起作用。我得到了解决方案,我在 Loaded 方法中分配了 textvalue。 private void UserControl_Loaded(object sender, RoutedEventArgs e) { txtName.Text = ParamValue.ToString(); }
    猜你喜欢
    • 1970-01-01
    • 2010-12-15
    • 2011-02-12
    • 1970-01-01
    • 2018-07-26
    • 1970-01-01
    • 2010-09-10
    • 2015-02-09
    • 1970-01-01
    相关资源
    最近更新 更多