【问题标题】:TemplateBinding: The member <> is not recognized or is not accessibleTemplateBinding:成员 <> 无法识别或不可访问
【发布时间】:2013-09-25 13:42:08
【问题描述】:

我是 C#、Windows 应用商店的新手,我正在努力解决一个问题。我想实现一些模板控件,它由一个带有进度环和文本的弹出窗口组成。

在模板类 (CustomProgressRing.cs) 中,我希望能够操作封闭的弹出窗口及其属性。 我通过将 TextBlock 的 Text 属性设置为 TempalteBinding 来成功使用 TextBlock,因此在类中我可以访问 TextBlock 的 Text 属性。 我想将 TemplateBinding 应用于弹出窗口的 IsOpen 道具,但出现错误: The member "IsOpen" is not recognized or is not accessible

下面是xaml:

 <Style TargetType="local:CustomProgressRingPopup">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="local:CustomProgressRingPopup">
                        <Border
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                            <Popup x:Name="ProgressRingPopup" x:Uid="LoggingInWaitingPopup" IsOpen="{TemplateBinding IsOpen}">
                                <Grid x:Name="gdChild" Width="Auto" Height="Auto" Background="#969696" >
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition/>
                                        <ColumnDefinition/>
                                    </Grid.ColumnDefinitions>
                                    <TextBlock x:Name="LoginProgressRingText" Height="Auto" Width="Auto" FontSize="20" Margin="20" VerticalAlignment="Center" Grid.Row="0" Grid.Column="1" Text="{TemplateBinding Text}"/>
                                </Grid>
                            </Popup>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

这是 CustomProgressRing.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Documents;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using System.Diagnostics;

// The Templated Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234235

namespace QSTLibrary.WIN8.Tools
{
    public sealed class CustomProgressRingPopup : Control
    {
        public CustomProgressRingPopup()
        {
            this.DefaultStyleKey = typeof(CustomProgressRingPopup);
        }



        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TextProperty =
                DependencyProperty.Register(
                    "Text", 
                    typeof(string), 
                    typeof(CustomProgressRingPopup), 
                    new PropertyMetadata("Void", new PropertyChangedCallback(OnTextChanged)));



        private void ProgressRingPopup_Opened(object sender, object e)
        {
            Debug.WriteLine("Popup opened");
        }

        private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
        {
            CustomProgressRingPopup instance = d as CustomProgressRingPopup;
            if (instance != null)
            {
                string newValue = e.NewValue as string;
                instance.Text = newValue;
                //instance.IsOpen = true; - not working
            }
        }
    }


}

为什么我不能将 templateBinding 设置为 Popup 的 IsOpen 属性?

【问题讨论】:

  • 你能发布你的 CustomProgressRingPopup 类的定义吗?
  • CustomProgressRingPopup 是否定义了 IsOpen 依赖属性?是派生Popup吗?
  • @Moozhe - 我已经发布了类定义
  • @nit - CustomProgressRingPopup 没有定义依赖属性。请在我的帖子中上课。我已经编辑了。

标签: c# wpf xaml


【解决方案1】:

当您从 Control 派生您的 CustomProgressRingPopup 时,这就是您没有获得 IsOpen 属性的原因。您应该在 CustomProgressRingPopup 中定义自己的 Dependancy 属性 IsOpen 来处理它,这有点工作。

Template binding searches the Property in the control that is being templated.

【讨论】:

  • 为什么我能够将模板绑定设置​​为 Text 属性(查看 xaml 中的 TextBlock)?是因为我在 .cs 文件中设置了对 text 属性的依赖关系吗?也许我应该像处理 Text 属性一样继续使用 IsOpen ...如果我扩展 Popup 而不是 Control,我仍然可以使用 CustomProgressRingPopup 作为模板对象吗?抱歉这个菜鸟问题,但我需要在很短的时间内完成一些事情。 (我是 Java 程序员(Android),我需要在 Windows 应用程序中处理一些事情)
  • 是的,您的控件中有 Text DP,这就是您可以模板绑定它的原因。在这种情况下,扩展 Popup 并不是一个好主意,因为您正在对其进行模板化..并且 Popup 没有暴露 Template 属性..所以最好继续创建 DP.. 我已经更新了答案
  • 你可以试试 IsOpen="{Binding IsOpen, RelativeSource={RelativeSource AncestorType={x:Type local:CustomProgressRingPopup}}}" 而不是模板绑定
  • 这很奇怪... :) RelativeSource={RelativeSource TemplatedParent}... 它也会给你错误吗?
  • 我建议您创建包含所有控件的用户控件并将该用户控件放置在弹出窗口中,而不是创建控件并使用弹出窗口对其进行模板化...然后您可以直接打开/关闭它弹出...其次,因为您将使用 usercontrol,您可以将其中的控件属性与 viewmodel 属性绑定,从而增加测试范围
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-07
  • 1970-01-01
  • 2015-02-13
  • 1970-01-01
  • 2012-11-09
  • 1970-01-01
相关资源
最近更新 更多