模板绑定:成员<>无法识别或无法访问

本文关键字:识别 访问 绑定 成员 | 更新日期: 2023-09-27 18:32:41

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

在模板类(CustomProgressRing.cs(中,我希望能够操作随附的弹出窗口及其属性。我成功地使用TextBlock,将其的文本属性设置为TempalteBinding,因此在类中我能够访问TextBlock的Text属性。我想将模板绑定应用于弹出窗口的 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
            }
        }
    }

}

为什么我不能将模板绑定设置为弹出窗口的 IsOpen 道具?

模板绑定:成员<>无法识别或无法访问

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

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