获取从MainPage.xaml.cs文件到Windows Phone中的MainPage.xaml文件的字符串变量的值

时间:2014-04-23 12:32:46

标签: c# wpf xaml windows-phone-8 data-binding

我创建了一个项目,其中MainPage.xaml.cs如下所示:

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(字符串)的值作为Source来获取Image的ImageShow?在没有创建任何新类的情况下,有没有办法从MainPage.xaml中使用DataBinding?

4 个答案:

答案 0 :(得分:2)

在codeBehind

中使用DataContext
public string ImagePath = "/Images/Flower.png";

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

    }

然后你在.xaml中使用你的绑定

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

答案 1 :(得分:2)

您需要将ImagePath转换为属性,因为您无法绑定到字段

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

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

为页面指定一些名称并将Image.Source绑定到ImagePath属性

<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>

答案 2 :(得分: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(""));

这将创建可用于绑定的属性,之后您可以在Image:

中绑定它
<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>

答案 3 :(得分:1)

将您的变量添加为代码隐藏中的资源:

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

myWindow.DataContext = ImagePath;

在XAML中访问:

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