2015-12-17 107 views
4

我有一個FlipView顯示小雕像。小雕像在其圖像中包含路徑從DataTemplate UWP綁定UserControl DP

將此屬性綁定到常規DataTemplate是可以的。 (下面的代碼工作正常)

</DataTemplate> 
    <Canvas x:Name="DefaultImageCanvas" Width="660" Height="372"> 
     <Image Name="imageFlip" Width="660" Height="372" Source="{Binding Path}" 
      Stretch="Uniform" /> 
    </Canvas> 
</DataTemplate> 

,但是改爲使用我的用戶時,它不工作了:

<DataTemplate> 
    <local:FigurineStickerUserControl Width="660" Height="372" 
             FigurinePath="{Binding Path}"/> 
</DataTemplate> 

的FigurinePath DP從未設置。 (如果我使用硬編碼字符串,它的罰款) 這裏是輸出錯誤:

Error: BindingExpression path error: 'Path' property not found on 'Com.Test.ViewModels.UserControl.FigurineStickerUserControlViewModel, eSmart.ViewModels, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. BindingExpression: Path='Path' DataItem='Com.Test.ViewModels.UserControl.FigurineStickerUserControlViewModel, Test.ViewModels, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'; target element is 'Com.Test.Views.FigurineStickerUserControl' (Name='pageRoot'); target property is 'FigurinePath' (type 'Object')

它看起來像DataTemplate中試圖指派雕像爲我的用戶的DataContext的,然後檢索從屬性我的UC的DataContext。但是我的UC有它自己的DataContext(它的ViewModel),我不想刪除它。

與WinRT/UWP不幸的是沒有FindAncestor技巧,我可以做綁定。我已經嘗試過了:(FlipFigurine是FlipView對象)

<local:FigurineStickerUserControl Width="660" Height="372" 
            FigurinePath="{Binding SelectedItem.Path, ElementName=FlipFigurine}"/> 

它不起作用。即使將DP更改爲對象並嘗試以下操作也無效,DP的設置者從不會被調用。儘管在日誌中沒有錯誤。

FigurinePath="{Binding SelectedItem, ElementName=FlipFigurine}" 

有什麼辦法來訪問實際的雕像對象,只是其路徑屬性綁定到我UC的FigurinePath財產?

回答

3

由於沒有FindAncestor,我認爲你唯一的希望是做小重構。下面是其中希望給你如何解決這個問題的想法樣本:

https://github.com/mikoskinen/uwpusercontrolbinding/tree/master

下面是該代碼的主要部分:

MainPage.xaml中

<DataTemplate> 
    <local:MyUserControl Width="660" Height="372" FigurinePath="{Binding Path}"/> 
</DataTemplate> 

MainPage.xaml.cs

private ObservableCollection<MyUserControlVm> coll; 
protected override void OnNavigatedTo(NavigationEventArgs e) 
{ 
    coll = new ObservableCollection<MyUserControlVm>(); 
    coll.Add(new MyUserControlVm("http://libcloud.readthedocs.org/en/latest/_images/azure.jpg")); 
    coll.Add(new MyUserControlVm("http://www.nimbo.com/wp-content/uploads/windows-azure-logo-nimbo1.png")); 

    this.Flip.ItemsSource = coll; 

    base.OnNavigatedTo(e); 
} 

MyUserControl.xaml

<Grid> 
    <Canvas Width="660" Height="372"> 
     <Image Width="660" Height="372" Source="{Binding FigurinePath}" Stretch="Uniform" /> 
    </Canvas> 
</Grid> 

MyUserControl.xaml.cs

public sealed partial class MyUserControl : UserControl 
{ 
    public static readonly DependencyProperty FigurinePathProperty = DependencyProperty.Register(
     "FigurinePath", typeof (Uri), typeof (MyUserControl), new PropertyMetadata(default(Uri))); 

    public Uri FigurinePath 
    { 
     get { return (Uri) GetValue(FigurinePathProperty); } 
     set { SetValue(FigurinePathProperty, value); } 
    } 

    public MyUserControl() 
    { 
     this.InitializeComponent(); 
     (this.Content as FrameworkElement).DataContext = this; 
    } 
} 

MyUserControlVM。CS

public class MyUserControlVm 
{ 
    public Uri Path { get; set; } 

    public MyUserControlVm(string url) 
    { 
     Path = new Uri(url); 
    } 

    public void VmAction() 
    { 

    } 
} 

對於涉及到例如一些參考,下面是一個article from Jerry Nixon.

+0

謝謝大家的Mikael!我沒有找到時間來測試它,但它看起來是唯一的途徑,即使我希望更優雅的方式(通過xaml)。只是一個細節:我沒有任何約束CB,因爲我們直接給虛擬機道路,DP現在是無用的嗎? – jujujuijk