简体   繁体   English

检测用户控件中更改的绑定属性

[英]Detect binding property changed within a user control

I am working on a Windows Store app in which I have a user control as a data template inside flipview. 我正在使用Windows Store应用程序,在其中我有一个用户控件作为flipview内的数据模板。

User Control: (ImagePage.xaml) 用户控件:(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>

User Control Class: (ImagePage.xaml.cs) 用户控件类:(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;
}

Everything is working fine EXCEPT the OnTextContent1Changed is not firing! 除OnTextContent1Changed未触发外,其他一切工作正常! so I dont know if this is the right way of doing things but basically I just want to switch an UI element within the user control ON or OFF depending on the data binding that is being fed into it. 所以我不知道这是否是正确的处理方式,但基本上我只是想根据用户输入的数据绑定将用户控件中的UI元素打开或关闭。

The TextBlock doesn't have a DataContext to find the DependencyProperty on. TextBlock没有DataContext来查找DependencyProperty If you give your Grid a name in ImagePage.xaml: 如果在ImagePage.xaml中为Grid命名:

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

Then you can set its DataContext in the ImagePage constructor in ImagePage.xaml.cs: 然后,可以在ImagePage.xaml.cs的ImagePage构造函数中设置其DataContext:

    public ImagePage()
    {
        InitializeComponent();

        MyGrid.DataContext = this;
    }

Which tells the Grid (and its decendents) to look for Dependency Properties on the ImagePage class. 告诉Grid (及其后代)在ImagePage类上查找依赖项属性。 With this, the Dependency Property should get bound correctly. 这样,Dependency属性应该正确绑定。 Another problem, though, is that you're telling the DependencyProperty that it is on an ImagePage with typeof(ImagePage) , but then casting it to a StackPanel, which will fail every time: 另外一个问题,不过,是你告诉DependencyProperty ,这是上ImagePagetypeof(ImagePage)但随后铸造一个StackPanel,将每一次失败:

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

You could fix this by giving a name to the StackPanel and referencing it directly in your .cs file. 您可以通过为StackPanel命名并直接在.cs文件中引用它来解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM