Mua*_*lig 0 c# silverlight windows-phone-7
我有一个简单的自定义控件:
namespace Application.Custom_Controls
{
public class ReadOnlyTextBox : TextBox
{
public ReadOnlyTextBox()
{
this.DefaultStyleKey = typeof(ReadOnlyTextBox);
this.IsReadOnly = true;
}
}
}
Run Code Online (Sandbox Code Playgroud)
并使自定义样式使控件看起来像TextBlock(在App.xaml中).
<Application
x:Class="Application.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:tb = "clr-namespace:Application.Custom_Controls"
>
<!--Application Resources-->
<Application.Resources>
<Style x:Key="ReadOnlyTextBox" TargetType="tb:ReadOnlyTextBox">
//...
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="tb:ReadOnlyTextBox">
//...
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
但是当我在我的应用程序中使用它时它根本不显示.如果我删除它,它作为普通TextBox显示this.DefaultStyleKey = typeof(ReadOnlyTextBox);.
如何将此样式应用于代码后面的自定义控件?
顺便说一句,这种风格在xaml中运行良好Style="{StaticResource ReadOnlyTextBox}",但在这种情况下我不能使用xaml.
提前致谢.
this.Style = (Style)Application.Current.Resources["ReadOnlyTextBox"];
Run Code Online (Sandbox Code Playgroud)
将此行添加到的构造函数 ReadOnlyTextBox