Furqan Safdar
Furqan Safdar

Reputation: 16728

WPF Element Binding not working in XAML

This should be fairly simple and straightforward but element binding is not working in XAML when using it from resource. It is working fine when using it directly in XAML.

Resources:

<Window.Resources>
    <StackPanel x:Key="panel">
        <CheckBox x:Name="chkDefaultValue" Content="Default Value"  
                  IsChecked="{Binding ElementName=txtDefaultValue, Path=Text.Length, Mode=OneWay}" />
        <TextBox x:Name="txtDefaultValue"
                  Text="{Binding DefaultValue, Mode=TwoWay, ValidatesOnDataErrors=True}"
                  IsEnabled="{Binding ElementName=chkDefaultValue, Path=IsChecked}" />
    </StackPanel>
</Window.Resources>

XAML:

<StackPanel>
    <!-- BINDING NOT WORKING -->
    <ContentControl Content="{StaticResource panel}" />

    <!-- BINDING WORKING HERE -->
    <CheckBox x:Name="chkDefaultValue" Content="Default Value"  
              IsChecked="{Binding ElementName=txtDefaultValue, Path=Text.Length, Mode=OneWay}" />
    <TextBox x:Name="txtDefaultValue"
              Text="{Binding DefaultValue, Mode=TwoWay, ValidatesOnDataErrors=True}"
              IsEnabled="{Binding ElementName=chkDefaultValue, Path=IsChecked}" />
</StackPanel>

How could i fix it?

Upvotes: 3

Views: 437

Answers (2)

Artyom Shmarlovsky
Artyom Shmarlovsky

Reputation: 51

And you can use ControlTemplate

<Window.Resources>
    <ControlTemplate x:Key="panel">
        <StackPanel>
            <CheckBox x:Name="chkDefaultValue"
                      Content="Default Value"
                      IsChecked="{Binding ElementName=txtDefaultValue,
                                          Path=Text.Length,
                                          Mode=OneWay}" />
            <TextBox x:Name="txtDefaultValue"
                     IsEnabled="{Binding ElementName=chkDefaultValue,
                                         Path=IsChecked}"
                     Text="{Binding DefaultValue,
                                    Mode=TwoWay,
                                    ValidatesOnDataErrors=True}" />
        </StackPanel>
    </ControlTemplate>
</Window.Resources>

and

<ContentControl Template="{StaticResource panel}" />

Upvotes: 1

Norbert Kovacs
Norbert Kovacs

Reputation: 149

You should use DataTemplate

<Window.Resources>
    <DataTemplate DataType="{x:Type ContentControl}" x:Key="panel">
       <StackPanel>
             <CheckBox x:Name="chkDefaultValue" Content="Default Value"  
              IsChecked="{Binding ElementName=txtDefaultValue, Path=Text.Length, Mode=OneWay}" />
             <TextBox x:Name="txtDefaultValue"
              Text="{Binding DefaultValue, Mode=TwoWay, ValidatesOnDataErrors=True}"
              IsEnabled="{Binding ElementName=chkDefaultValue, Path=IsChecked}" />
       </StackPanel>
    </DataTemplate>
</Window.Resources>

and

<ContentControl ContentTemplate="{StaticResource panel}" />

didn't check, but probably works

Upvotes: 2

Related Questions