2014-07-15 44 views
1

我正在使用Windows應用商店應用程序,其中我有一個用戶控件作爲flipview內的數據模板。檢測用戶控件內更改的綁定屬性

用戶控制:(ImagePage.xaml)

<UserControl 
    x:Name="userControl" 
    x:Class="MWC_online.Classes.ImagePage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:MWC_online.Classes" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    d:DesignHeight="768" 
    d:DesignWidth="1366"> 

    <Grid Background="#FFF0F0F0" Margin="4,0"> 
    ... 
     <Image Source="{Binding Img}" Stretch="None" HorizontalAlignment="Left" VerticalAlignment="Top" /> 
     <StackPanel x:Name="stackPanel" HorizontalAlignment="Left" Margin="984,83,0,0" Width="325"> 
      <Grid Background="{Binding Colour}"> 
       <TextBlock Margin="30,30,30,15" Text="{Binding TextContent1}" FontWeight="Light" TextWrapping="Wrap" Foreground="#FF00ABE8" FontSize="29" /> 
      </Grid> 
      <Grid Background="{Binding Colour}"> 
       <TextBlock Margin="30,10,30,30" Text="{Binding TextContent2}" TextWrapping="Wrap" Foreground="#FF606060" FontSize="17" /> 
      </Grid> 
     </StackPanel> 
    </Grid> 
</UserControl> 

用戶控件類:(ImagePage.xaml.cs)

private static void OnTitleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){ 
    StackPanel stackPanel = (StackPanel)d; 
    stackPanel.Visibility = Visibility.Collapsed; 
} 

public string TextContent1 
{ 
    get { return (string)GetValue(TextContent1Property); } 
    set { SetValue(TextContent1Property, value); } 
} 
public static readonly DependencyProperty TextContent1Property = 
    DependencyProperty.Register("TextContent1", typeof(string), typeof(ImagePage), new PropertyMetadata("", new PropertyChangedCallback(OnTextContent1Changed))); 

private static void OnTextContent1Changed(DependencyObject d, DependencyPropertyChangedEventArgs e) 
{ 
    // what I want to do is if TextContent1 or TextContent2 has no value 
    // turn the stackpanel visibility to collapsed 
    StackPanel stackPanel = (StackPanel)d; 
    stackPanel.Visibility = Visibility.Collapsed; 
} 

一切除OnTextContent1Changed工作正常不點火!所以我不知道這是否是正確的做事方式,但基本上我只是想在用戶控件的ON或OFF內切換UI元素,具體取決於正在輸入的數據綁定。

回答

0

TextBlock沒有DataContext查找DependencyProperty。如果你給你的Grid在ImagePage.xaml名稱:

<Grid Background="#FFF0F0F0" Margin="4,0" x:Name="MyGrid"> 

然後你可以設置它的DataContext在ImagePage構造函數ImagePage.xaml.cs:

public ImagePage() 
    { 
     InitializeComponent(); 

     MyGrid.DataContext = this; 
    } 

它告訴Grid(及其派生)在ImagePage類中查找依賴屬性。有了這個,依賴屬性應該正確綁定。另外一個問題,不過,是你告訴DependencyProperty,這是上ImagePagetypeof(ImagePage),但隨後鑄造一個StackPanel,將每一次失敗:

StackPanel stackPanel = (StackPanel)d; // Throws System.InvalidCastException 

您可以通過給解決這個問題名稱添加到StackPanel並直接在.cs文件中引用它。

+0

謝謝@Thanatos,很抱歉,但我可能並不清楚我有的問題,我可以解決UI元素的問題,(我的不好,我沒有展示我的整個ImagePage類,我實際上已經聲明瞭我的DataContext那麼)但是我得到的問題是'OnTextContent1Changed'從不被調用,當頁面被渲染並且綁定數據被輸入時。我在'OnTextContent1Changed'方法中有一個Debug.WriteLine,但它從來沒有被調用過。 –

+0

您是否在提供綁定數據之前或之後設置DataContext?如果您在頁面呈現後的某個時間點對屬性進行更改,是不是稱爲「OnTextContent1Changed」?我也會嘗試在TextContent1的get/set塊中放置斷點或調試語句,並確保它們正確觸發。 – Thanatos

+0

謝謝@Thanatos,最後我通過檢查onLoaded中指定文本塊的值是否爲空來稍微改變我的結構,這是一個簡單的解決方案,我想不出來,我想我太掛了關於文本塊綁定的想法。 –

相關問題