3

我是 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 属性?

4

1 回答 1

3

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

Template binding searches the Property in the control that is being templated.
于 2013-09-26T10:14:03.793 回答