【问题标题】:WPF Element Binding not working in XAMLWPF 元素绑定在 XAML 中不起作用
【发布时间】:2016-02-23 11:02:16
【问题描述】:

这应该相当简单明了,但是当从资源中使用元素绑定时,它在 XAML 中不起作用。直接在 XAML 中使用时效果很好。

资源:

<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>

我该如何解决?

【问题讨论】:

    标签: wpf xaml binding


    【解决方案1】:

    你应该使用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>
    

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

    没有检查,但可能有效

    【讨论】:

      【解决方案2】:

      你可以使用 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>
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-09
        • 1970-01-01
        • 1970-01-01
        • 2021-08-16
        • 1970-01-01
        • 2016-04-10
        • 2020-11-01
        • 2014-06-13
        相关资源
        最近更新 更多