【问题标题】:How to dynamically change the properties of a text box in xaml如何动态更改xaml中文本框的属性
【发布时间】:2012-01-05 12:01:28
【问题描述】:

我使用 xaml 创建了一个文本框。我有一个名为styles.xaml 的文件,其中包含文本框的所有属性。现在我想通过styles.xaml动态更改属性(高度、宽度等)。

【问题讨论】:

    标签: silverlight xaml styles


    【解决方案1】:

    您可以使用文本框的Style 属性。

    myTextbox.Style = Application.Current.Resources["NewTextboxStyle"] as Style;
    

    【讨论】:

      【解决方案2】:
      1. 将资源字典引用添加到您的 app.xaml

        <Application.Resource>
        
           <ResourceDictionary Source="styles.xaml"/> 
        
        </Application.Resources>
        
      2. 在您的代码隐藏中使用 this.Resources 来访问您的样式资源。

        Style txtStyle = App.Current.Resources["TextBoxStyle"] as Style;
        
      3. 设置文本框的样式

        testTxtBox.Style=txtStyle
        

      【讨论】:

      • 谢谢,这是一个开始,但我无法从后面的代码访问 setter 中的 width 属性。我的样式文件有这样的 '' 我想改变宽度的值。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-24
      • 2015-07-16
      • 2015-03-10
      • 2019-08-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多