wpf TemplateBinding:成员 <> 无法识别或不可访问

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19006619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:43:27  来源:igfitidea点击:

TemplateBinding: The member <> is not recognized or is not accessible

c#wpfxaml

提问by Alexandru Circus

I'm new to C#, windows apps store and I'm struggling with a problem. I want to implement some template control which consist of a popup with a progress ring and a text.

我是 C#、Windows 应用程序商店的新手,我遇到了一个问题。我想实现一些模板控件,其中包含一个带有进度环和文本的弹出窗口。

In the Template class (CustomProgressRing.cs) I want to be able to manipulate the enclosed popup and it's properties. I succeeded with the TextBlock by setting it's Text prop as TempalteBinding, so in the class I am able to access the TextBlock's Text property. I want to apply the TemplateBinding to the IsOpen prop of the popup, but I get the error: The member "IsOpen" is not recognized or is not accessible

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

Below is the xaml:

下面是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>

Here is the CustomProgressRing.cs:

这是 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
            }
        }
    }


}

Why I can't set templateBinding to the IsOpen prop of the Popup?

为什么我不能将 templateBinding 设置为 Popup 的 IsOpen 道具?

采纳答案by Nitin

As you are deriving your CustomProgressRingPopupfrom Controlthat is the reason you are not getting IsOpenproperty. You should define your own Dependancy property IsOpen in CustomProgressRingPopup to handle it, which is bit of the work.

因为你是CustomProgressRingPopupControl那里得到的,这就是你没有得到IsOpen财产的原因。您应该在 CustomProgressRingPopup 中定义您自己的 Dependancy 属性 IsOpen 来处理它,这是一些工作。

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