【问题标题】:Binding property of usercontrol to data将用户控件的属性绑定到数据
【发布时间】:2015-06-13 04:50:15
【问题描述】:

我不确定要使用的正确术语。大约一年前,我创建了一个 Windows 应用商店应用程序,主页是由 Visual Studio 创建的,我从未对其进行过太大更改。它使用一个工作正常的视图模型,但我不知道如何解决问题。总之……

页面使用 GridView 来显示 CollectionViewSource 元素的内容以引用 ObservableCollection。这一切都很好。其中一个数据项的 DataTemplate 现在如下所示:

<DataTemplate x:Key="TopImageTileTemplate">
    <Grid MinHeight="135" Width="350" Margin="0" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="135"/>
        </Grid.RowDefinitions>

        <TextBlock Text="{Binding Path=ImagePath}" FontSize="33"/>

        <usercontrols:WaitingImageControl SourcePath="{Binding Path=ImagePath}" Grid.Row="0" Width="350" Height="165" HorizontalAlignment="Center" VerticalAlignment="Stretch" AutomationProperties.Name="{Binding Title}" Visibility="{Binding TypeDescription, RelativeSource={RelativeSource Mode=TemplatedParent}, Converter={StaticResource TextToVis}}"/>

        <usercontrols:WaitingImageControl SourcePath="XXX" Grid.Row="0" Width="350" Height="165" HorizontalAlignment="Center" VerticalAlignment="Stretch" AutomationProperties.Name="{Binding Title}" Visibility="{Binding TypeDescription, RelativeSource={RelativeSource Mode=TemplatedParent}, Converter={StaticResource TextToVis}}"/>

        <ProgressRing Opacity="0.5" Foreground="#FF8A57FF" Grid.Row="0" Name="TheProgressControl" IsActive="True" Height="32" Width="32" Background="Transparent" VerticalAlignment="Center" HorizontalAlignment="Center"/>

        ...

    </Grid>
</DataTemplate>

我遇到的问题是,它的数据项包含一个名为 ImagePath 的字符串,我想将它传递给 WaitingImageControl 用户控件,但它不起作用。 TextBlock 工作正常,文本显示 ImagePath 字符串就好了。第二个 WaitingImageControl 工作正常,处理 SourcePath 的代码也确实通过了“XXX”。但是第一个 WaitingImageControl 永远不会从数据项中传递 ImagePath 值。

这是某种绑定问题,我对绑定知之甚少,以至于我什至无法确定要尝试什么(或在这个问题中显示什么)。鉴于 TextBlock 绑定有效且第二个 WaitingImageControl 绑定有效,我不知所措。

这是 SourcePath 属性的 WaitingImageControl 代码:

    public static readonly DependencyProperty SourcePathProperty = 
        DependencyProperty.Register("SourcePath", typeof(string), typeof(WaitingImageControl), new PropertyMetadata(""));

    public string SourcePath
    {
        get { return m_SourcePath; }
        set 
        {
            if( string.IsNullOrEmpty( value ) )
                return;

            m_SourcePath = value;
            ResourcesStore Store = new ResourcesStore();
            if( Store.Count() == 0 )
            {
                var IgnoreMe = CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( CoreDispatcherPriority.Normal, () =>
                {
                    // No progress and no image...
                    TheProgressControl.Visibility = Visibility.Collapsed;
                    TheImage.Visibility = Visibility.Collapsed;
                } );
                return;
            }
            ResourceItem Item = Store.getItemByFilename( m_SourcePath );

            LocalInboxService.Instance.InboxStatusChanged -= InboxStatusChanged;
            InboxStatusChanged( null );
            LocalInboxService.Instance.InboxStatusChanged += InboxStatusChanged;
        }
    }

该代码应该在图像下载后显示 Image 元素并隐藏 ProgressRing 元素。

当 ImagePath 自动传递给 TextBlock 时,数据项的代码同样可以正常工作:

    public string ImagePath
    {
        get
        {
             return this._imagePath;
        }

        set
        {
            this._imagePath = value;
            this.SetProperty(ref this._imagePath, value);
       }
    }

感谢任何帮助使 ImagePath 到 SourcePath 绑定(如下)的工作:

    <usercontrols:WaitingImageControl SourcePath="{Binding Path=ImagePath}"
     Grid.Row="0" Width="350" Height="165" HorizontalAlignment="Center"
     VerticalAlignment="Stretch" AutomationProperties.Name="{Binding Title}"
     Visibility="{Binding TypeDescription, RelativeSource={RelativeSource
     Mode=TemplatedParent}, Converter={StaticResource TextToVis}}"/>

【问题讨论】:

    标签: c# xaml mvvm binding winrt-xaml


    【解决方案1】:

    经过数小时的搜索,我找到了 StackOverflow 对类似问题的回答。答案是向 Propertymetadata 添加一个 PropertyChanged 函数。我还不确定这实际上意味着什么或为什么只需要在这里,但它可以正常工作:

        public static readonly DependencyProperty SourceImageResourceIdProperty = 
            DependencyProperty.Register("SourceImageResourceId", typeof(string), typeof(WaitingImageControl), new PropertyMetadata( string.Empty, OnSourcePathPropertyChanged ));
    
    
        private static void OnSourcePathPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as WaitingImageControl).SourceImageResourceId = e.NewValue.ToString();
        }
    

    调用 OnSourcePathPropertyChanged 函数并按应有的方式设置属性。

    现在我只希望它不是其他 20 个实验中的一个,实际上解决了这个问题!

    【讨论】:

    猜你喜欢
    • 2013-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-29
    • 1970-01-01
    相关资源
    最近更新 更多