繁体   English   中英

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

[英]Detect binding property changed within a user control

我正在使用Windows Store应用程序,在其中我有一个用户控件作为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未触发外,其他一切工作正常! 所以我不知道这是否是正确的处理方式,但基本上我只是想根据用户输入的数据绑定将用户控件中的UI元素打开或关闭。

TextBlock没有DataContext来查找DependencyProperty 如果在ImagePage.xaml中为Grid命名:

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

然后,可以在ImagePage.xaml.cs的ImagePage构造函数中设置其DataContext:

    public ImagePage()
    {
        InitializeComponent();

        MyGrid.DataContext = this;
    }

告诉Grid (及其后代)在ImagePage类上查找依赖项属性。 这样,Dependency属性应该正确绑定。 另外一个问题,不过,是你告诉DependencyProperty ,这是上ImagePagetypeof(ImagePage)但随后铸造一个StackPanel,将每一次失败:

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

您可以通过为StackPanel命名并直接在.cs文件中引用它来解决此问题。

暂无
暂无

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

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