【问题标题】:bind value to textblock inside resourcedictionary将值绑定到资源字典中的文本块
【发布时间】:2017-05-22 19:28:47
【问题描述】:

我有以下资源字典。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:samplePrjkt"
                    >

    <ToolBar x:Key="MyToolbar" Height="120">
        <!--Template-->
        <GroupBox Header="Template" Style="{StaticResource ToolbarGroup}" Margin="3">
            <StackPanel Grid.Row="1" Orientation="Horizontal">
                <StackPanel Orientation="Vertical" Margin="0,2,0,2">
                    <TextBlock Text="{Binding TextValue}"></TextBlock>
                </StackPanel>
            </StackPanel>
        </GroupBox>
    </ToolBar>
</ResourceDictionary>

用于以下 WPF 用户控件的资源字典。

<UserControl x:Class="Sampleprjkt.sample.sampleWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Sampleprjkt"            
       >
    <Grid Margin="10">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="29*"/>
            <RowDefinition Height="107*"/>
        </Grid.RowDefinitions>

        <ContentControl Content="{StaticResource MyToolbar}"/>

    </Grid>
</UserControl>

我正在尝试将值绑定到 WPF 用户控件构造函数中的此文本块,如下所示

    public partial class SampleWindow : UserControl
    {

        private string _textValue;

        public string TextValue
        {
            get { return _textValue; }
            set
            {
                _textValue = value;

            }
        }

        public SampleWindow()
        {
            InitializeComponent();
            _textValue = "XXXXX";


        }   

    }

但是一旦我运行它,我可以看到 "XXXXX" 值没有设置为 &lt;TextBlock Text="{Binding TextValue}"&gt;&lt;/TextBlock&gt; ,我在这里错过了什么?

【问题讨论】:

  • 我想你可以通过添加 relativeSource:Text="{Binding TextValue, RelativeSource={RelativeSource AncestorType=SampleWindow }}" 来修复绑定。但整体设计值得商榷
  • @ASh - 我同意这个设计是有问题的 - 但我们不是通过在这里添加一个 RelativeSource 绑定来进一步复杂化这个问题吗?是否在假设父级始终是在推荐的隔离 ResourceDictionary 中定义的控件实例的 UserControl/SampleWindow 的假设下工作?

标签: c# wpf xaml resourcedictionary


【解决方案1】:

您的 ContentControl 缺少一个为空的 DataContext。 Binding 将始终引用 DataContext 中的对象,因此 Binding 将找不到 TextValue。

您可以简单地将 UserControl 的 DataContext 设置为自身:

public SampleWindow()
{
    InitializeComponent();
    _textValue = "XXXXX";

    this.DataContext = this;
}  

DataContext 被继承到 TextBlock,它现在将显示文本。

【讨论】:

    【解决方案2】:

    您在 XAML 中定义的绑定路径(在您的情况下为“TextValue”)是指元素的当前DataContext 的属性名称(在您的情况下为TextBlock)或绑定。

    这意味着您应该按照@Sharada Gururaj 的建议设置DataContext

    public SampleWindow()
    {
        InitializeComponent();
        _textValue = "XXXXX";
        DataContext = this;
    }
    

    ...或指定绑定的显式来源:

    <TextBlock Text="{Binding Path=TextValue, Source={RelativeSource AncestorType=UserControl}}"></TextBlock>
    

    【讨论】:

      猜你喜欢
      • 2011-11-04
      • 1970-01-01
      • 1970-01-01
      • 2011-12-14
      • 1970-01-01
      • 2012-05-07
      • 2023-04-01
      • 1970-01-01
      • 2016-08-28
      相关资源
      最近更新 更多