Tuesday, September 11, 2007

Pants WPF Error

http://blogs.charteris.com/blogs/patl/archive/2007/03.aspx

Thanks Patrick for the post.

<Style x:Key="DisplayOnly" TargetType="{x:Type TextBox}"   BasedOn="{StaticResource {x:Type Control}}">
  <Setter Property="Control.BorderThickness" Value="0,0,0,0" />
  <Setter Property="TextBox.IsReadOnly" Value="True" />
  <Setter Property="Control.Background" Value="Transparent"/>
</Style>
 
<Style x:Key="DateDueStyle" BasedOn="{StaticResource DisplayOnly}">
  <Setter Property="Control.Foreground" Value="Red"/>
  <Setter Property="Control.FontWeight" Value="Bold"/>
</Style>


Trying to base a style on another style, was giving me the rather cryptic error;
{"Cannot convert the value in attribute 'Style' to object of type 'System.Windows.Style'. Can only base on a Style with target type that is base type 'IFrameworkInputElement'. Error at object 'System.Windows.Controls.TextBox' in markup file 'xxx;xamlfile.xaml' Line 111 Position 47."}

I found Patrick's post (Linked top of this post) and initially was going to move the DisplayOnly style stuff into the local DateDueStyle style. But that wasn’t right, I wanted the DisplayOnly to remain where it was (It's in a different assembly and gets loaded into the ResourceDictionary).

Adding a TargetType fixed the problem. So it looks like the TargetStyle does not come down to your style when you use BasedOn. The real problem was that it wasnt set.


<Style x:Key="DateDueStyle" TargetType="{x:Type TextBox}" BasedOn="{StaticResource DisplayOnly}">
  <Setter Property="Control.Foreground" Value="Red"/>
  <Setter Property="Control.FontWeight" Value="Bold"/>
</Style>

3 comments:

Anonymous said...

Thanks! V useful, probably saved me an hour!

Anonymous said...

Thanks...I was solving the issue whole night.

Anonymous said...

Saved me too. Thanks!