2014-04-23 50 views
1

我創建了一個項目中MainPage.xaml.cs中是這樣的:獲得價值文件到MainPage.xaml中文件的Windows Phone

using System; 
using System.Collections.Generic; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Navigation; 
using Microsoft.Phone.Controls; 
using Microsoft.Phone.Shell; 

namespace DemoApp 
{ 
    public partial class MainPage : PhoneApplicationPage 
    { 
     public string ImagePath = "/Images/Flower.png"; 

     public StartPage() 
     { 
      InitializeComponent(); 

     } 
    } 
} 

而且我創建了MainPage.xaml中像下面這樣:

<phone:PhoneApplicationPage 
    x:Class="DemoApp.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    FontFamily="{StaticResource PhoneFontFamilyNormal}" 
    FontSize="{StaticResource PhoneFontSizeNormal}" 
    Foreground="{StaticResource PhoneForegroundBrush}" 
    SupportedOrientations="Portrait" Orientation="Portrait" 
    mc:Ignorable="d" 
    shell:SystemTray.IsVisible="True">  

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
     <Image x:Name="ImageShow" Source=""/> 
    </Grid> 

</phone:PhoneApplicationPage> 

現在的問題是,有沒有什麼辦法讓ImagePath的(字符串)到名爲ImageShow作爲源的圖像的價值?在這種情況下,有沒有辦法從MainPage.xaml中使用DataBinding而不創建任何新類?

回答

2

您需要ImagePath轉化爲財產,你是無法綁定到字段

public partial class MainPage : PhoneApplicationPage 
{ 
    public string ImagePath { get; set; } 

    public MainPage() 
    { 
     ImagePath = "/Images/Flower.png"; 
     InitializeComponent(); 
    } 
} 

給予頁面一些的名字和綁定Image.SourceImagePath財產

<phone:PhoneApplicationPage ... x:Name="myPage">   
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
     <Image x:Name="ImageShow" Source="{Binding ElementName=myPage, Path=ImagePath}"/> 
    </Grid> 
</phone:PhoneApplicationPage> 
+0

謝謝你。你救了我的命。 :)我從你身上學到了一件非常重要的事情。非常感謝。 :) @dkozl –

+1

沒問題@TanvirSourov,很高興它幫助。您可以找到更多關於綁定源[這裏](http://msdn.microsoft.com/en-us/library/ms743643(v = vs.110).aspx) – dkozl

+0

謝謝。我一定會更多地研究數據綁定。 :) –

1

您應該創建的DependencyProperty

public string ImagePath 
{ 
    get { return (string) GetValue(ImagePathProperty); } 
    set { SetValue(ImagePathProperty, value); } 
} 

public static readonly DependencyProperty ImagePathProperty = 
     DependencyProperty.Register("ImagePath", typeof(string), typeof(MainPage), new PropertyMetadata("")); 

這將創建一個可用的屬性綁定,在這之後你可以給它綁定在圖片:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
    <Image x:Name="ImageShow" Source="{Binding Path=ImagePath, RelativeSource={RelativeSource AncestorType=PhoneApplicationPage}}"/> 
</Grid> 
2

使用的DataContext在你的代碼隱藏

public string ImagePath = "/Images/Flower.png"; 

    public StartPage() 
    { 
     InitializeComponent(); 
     this.DataContext=this; 

    } 

然後你在你的.xaml中使用你的綁定

<Image x:Name="ImageShow" Source="{Binding ImagePath}"/> 
1

添加您的變量作爲資源來自代碼隱藏:

myWindow.Resources.Add("ImagePath", "/Images/Flower.png"); 

myWindow.DataContext = ImagePath; 

訪問XAML:

<Image x:Name="ImageShow" Source={Binding Path=ImagePath}"/>