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

时间:2013-09-25 13:42:09

标签: c# wpf xaml

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

在Template类(CustomProgressRing.cs)中,我希望能够操作附带的弹出窗口及其属性。 我通过将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 prop?

1 个答案:

答案 0 :(得分:3)

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

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