我是C#的新手,Windows应用程序商店存储,并且正遇到问题.我想实现一些模板控件,其中包括带有进度环和文本的弹出窗口.
在Template类(CustomProgressRing.cs)中,我希望能够处理封闭的弹出窗口及其属性.
我通过将TextBlock的Text属性设置为TempalteBinding来成功实现了TextBlock,因此在该类中,我可以访问TextBlock的Text属性.
我想将TemplateBinding应用于弹出窗口的IsOpen属性,但出现错误:
成员“ IsOpen”未被识别或无法访问
以下是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道具?
解决方法:
当您从Control派生CustomProgressRingPopup时,这就是您没有获得IsOpen属性的原因.您应该在CustomProgressRingPopup中定义自己的Dependency属性IsOpen来处理它,这只是工作量.
Template binding searches the Property in the control that is being templated.