2013-10-01 40 views
0

問題我有一個生成與用戶控件的GridView代碼:與綁定到用戶控件

<ListView x:Name="ListView" SelectionMode="None"> 
      <ListView.ItemTemplate> 
       <DataTemplate> 
        <Grid> 
          <UserControls:ItemTemplateControl Parametr="XXXXXXX"/> 
        </Grid> 
       </DataTemplate> 
      </ListView.ItemTemplate> 
     </ListView> 

列表視圖工作正常,但我不能送什麼東西給我的用戶。 用戶控件代碼:

public sealed partial class ItemTemplateControl : UserControl 
    { 
     public string Parametr 
     { 
      get 
      { 
       return (string)GetValue(ParametrProperty); 
      } 
      set 
      { 
       SetValue(ParametrProperty, value); 
      } 
     } 
     public static DependencyProperty ParametrProperty = DependencyProperty.Register("Parametr", typeof(string), typeof(ItemTemplateControl), new PropertyMetadata("")); 


     public ItemTemplateControl() 
     { 
      this.InitializeComponent(); 
      post_text.Text = Parametr; 
      Get(); 
     } 

此代碼不能正常工作!我現在不是現在問題在哪裏?

對不起,我的英語

<Page 
    x:Class="App13.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:App13" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:UserControls="using:App13.UserControls" 
    mc:Ignorable="d"> 
+0

請提供該問題的XAML頭。你的問題可能是你沒有在xaml中正確打開'ItemTemplateControl'命名空間。 –

+0

沒有錯誤代碼 –

+0

你在哪裏設置列表視圖 – Nitin

回答

1

我想你想註冊一個回調時,依賴屬性Parametr變化。要做到這一點,你必須:

public static DependencyProperty ParametrProperty = DependencyProperty.Register("Parametr", typeof(string), typeof(ItemTemplateControl), new PropertyMetadata("", ParametrChanged)); 

而在ParametrChanged方法定義了一些邏輯

private static void ParametrChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
{ 
     var sender = d as ItemTemplateControl; 
     if (sender == null) return; 

     var newValue = e.NewValue as string; 
     sender.post_text.Text = newValue; 
} 
+0

我可以使用ParametrChanged方法的價值,但我可以用它我自己的方法? –

+0

我會做相反:叫「我自己的方法」從ParametrChanged(例如在ItemTemplateControl類的方法)。這有意義嗎? 要擴大這樣的:你可以在你的類中定義的私有方法,並從回調調用它的'sender'。在該私有方法中,您可以訪問屬性'Parametr'並且它應該包含更新的值。 – DenisPostu