2013-09-25 73 views
3

我是C#,Windows應用商店的新手,我正在努力解決問題。我想實現一些模板控件,它由一個帶有進度環和文本的彈出框組成。TemplateBinding:成員<>無法識別或無法訪問

在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支柱?

+2

你可以發佈CustomProgressRingPopup類的定義嗎? –

+2

CustomProgressRingPopup定義了IsOpen依賴項屬性?它是派生Popup? – Nitin

+0

@Moozhe - 我發佈了類定義 –

回答

3

由於您從Control推導出您的CustomProgressRingPopup,這就是您沒有獲得IsOpen財產的原因。您應該在CustomProgressRingPopup中定義您自己的Dependancy屬性IsOpen來處理它,這是工作的一部分。

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

爲什麼我能夠將模板綁定到Text屬性(查看xaml中的T​​extBlock)?這是因爲我將依賴性設置爲.cs文件中的文本屬性?也許我應該像使用Text屬性一樣繼續使用IsOpen ...如果我擴展Popup而不是Control,我仍然可以使用CustomProgressRingPopup作爲模板aobject? Sry爲這個noob問題,但我需要在很短的時間內完成的東西。 (我是一個Java程序員(Android),我需要處理Windows應用程序中的東西) –

+1

是的,你有Text DP在你的控制這就是爲什麼你可以模板綁定它。在這種情況下擴展彈出不會是一個好主意,因爲你是模板化的...而Popup沒有模板屬性暴露..所以最好去創建DP ..我已經更新了答案 – Nitin

+0

我已經添加了dp在類中,現在我可以在xaml中設置模板綁定,但它在啓動時崩潰,並且出現錯誤:「未能分配給屬性」Windows.UI.Xaml.Controls.Primitives.Popup.IsOpen「。也許我不允許使用IsOpen作爲依賴項。 –

相關問題