Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Element Binding not working in XAML

Tags:

binding

wpf

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?

like image 516
Furqan Safdar Avatar asked Dec 08 '25 06:12

Furqan Safdar


2 Answers

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

like image 185
Norbert Kovacs Avatar answered Dec 09 '25 18:12

Norbert Kovacs


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}" />
like image 42
Artyom Shmarlovsky Avatar answered Dec 09 '25 20:12

Artyom Shmarlovsky