我不确定要使用的正确术语。我大约一年前创建了一个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}}"/>
答案 0 :(得分:0)
经过几个小时的搜索,我发现了StackOverflow对类似问题的回答。答案是将PropertyChanged函数添加到Propertymetadata。我不确定这实际意味着什么,或者为什么只在这里需要,但它运作正常:
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个实验中的一个实际上解决了这个问题!