0

I'm trying to create a style that will attach a Converter to it so that when I use this style it will automatically use the converter. The problem I'm having is that if I don't set a path in the style, the compiler does not like it. I don't want to set the "Path" property of the binding in the style because I want to choose the path at design time. Not all control will automatically use the same Path name.

Here is my example :

<Style x:Key="SomeCustomTextBox" BasedOn="{StaticResource {x:Type TextBox}}" TargetType="{x:Type TextBox}">
    <Setter Property="Text">
        <Setter.Value>
            <Binding>
                <Binding.Path>SomePath</Binding.Path>
                <Binding.Converter>
                    <Converters:SomeIValueConverter/>
                </Binding.Converter>
            </Binding>
        </Setter.Value>
    </Setter>
</Style>

Also, if I use the style in the like in the next line (here bellow) of my xaml code, it automatically overides the whole binding not just the binding path.

<TextBox Height="28" Name="someNameThatOveridesDefaultPath" Style="{StaticResource SomeCustomTextBox}" MaxLength="5" />

Is it possible somehow to do something like this?

Thanks! Patrick Miron

Patrick Miron
  • 221
  • 3
  • 12
  • Am I right in thinking that you want the textboxes name to become the path for the binding inside the style? – Andy Jun 15 '12 at 14:48
  • something like that could be possible with an attached behavior but this would mean you build the binding in code. Also the last part i don't understand. – dowhilefor Jun 15 '12 at 14:48
  • The path does not have to be the textbox name, what I want is to be able to set the path at design time when I create the textbox and assign the style without overiding the Converter that I set in the style. Right now, – Patrick Miron Jun 15 '12 at 14:53
  • that's what my above code is doing. It seems to loose the converter in the style I created. I don't want to have to rewrite the converter in each of the customText boxes. – Patrick Miron Jun 15 '12 at 14:54

1 Answers1

0

try using an attached property like this

public class AttachedProperties
{
    public static readonly DependencyProperty RawTextProperty = DependencyProperty.RegisterAttached(
        "RawText",
        typeof(string),
        typeof(AttachedProperties));

    public static void SetRawText(UIElement element, string value)
    {
        element.SetValue(RawTextProperty, value);
    }

    public static string GetRawText(UIElement element)
    {
        return element.GetValue(RawTextProperty) as string;
    }
}

then in your XAML

<Style x:Key="SomeCustomTextBox" BasedOn="{StaticResource {x:Type TextBox}}" TargetType="{x:Type TextBox}">
    <Setter Property="Text" Value="{Binding RelativeSource={RelativeSource Mode=Self}, 
    Path=local:AttachedProperties.RawText, Converter=Converters:SomeIValueConverter}">
    </Setter>
</Style>


<Grid>
    <TextBox Style="{StaticResource SomeCustomTextBox}" 
                local:AttachedProperties.RawText="test text" />
</Grid>

I havent tested the code, but this is the idea

Dean Chalk
  • 20,076
  • 6
  • 59
  • 90
  • I see what your doing in your code, but at this point should I not just make a custom control? I know it may be shorter your way but I was trying to avoid code behind in my resources dictionaries... Thanks alot... I will give you full points but I wouldn't mind if there was a no code solution. Do you think your answer is the only way to do this? – Patrick Miron Jun 15 '12 at 15:15
  • The only other way is to have a different style for each possible binding path – Dean Chalk Jun 15 '12 at 15:21
  • Also, attached properties should be in their own class files and not in code-behind for resource files. Attached properties should be extensively leveraged where possible, as they avoid the need to author custom controls in many scenarios. They fully comply with MVVM – Dean Chalk Jun 15 '12 at 15:22
  • I'm getting an error saying "a property element cannot be the direct content of another element"... I'm still researching online what it means to my particular problem... The description on MSDN just says that the XAML is not written properly... Pfff... – Patrick Miron Jun 18 '12 at 21:08
  • I think I figured out what was the problem. Something with my long-hand XAML... I will post another question to where I can find the longhand XAML for all properties and setters... I have to learn that properly, its driving me nuts. – Patrick Miron Jun 18 '12 at 21:57
  • OOps... It compiles fine but it's not doing what its supposed to do. I create my style in a resource dictionary so the local:AttachedProperties.RawText does not do anything on my xaml page when I set it... Its probably creating another instance... I know I should know this... but something is making my thinking go in circles... Any help would be appreciated. – Patrick Miron Jun 18 '12 at 22:05