使用MVVM绑定自定义ImageSource路径

时间:2014-02-19 09:57:43

标签: c# windows mvvm windows-runtime winrt-xaml

我正在开发一个购物目录,我是MVVM中的菜鸟,所以请帮助我,是否可以在ImageSource中绑定自定义路径?这是我的基本代码:

观点?

public class CakeData
        {
            public string Cakename { get; set; }
            public ImageSource _imageSource { get; set; }

        }

和DataSource:

 public List<CakeData> _cakeData;
        public List<CakeData> CakeData
        {
            get
            {
                return _cakeData;
            }
            set
            {

                SetProperty(ref _cakeData, value);
            }
        }



    public CakeDataSource()
            {
                string commander = "select * from tblCakes where caketype='Cakes'";

                MySqlConnection connection = new MySqlConnection(GetConnectionString());
                MySqlCommand ds = new MySqlCommand(commander, connection);
                connection.Open();
                MySqlDataReader dt = ds.ExecuteReader();
                CakeData = new List<CakeData>();
                while (dt.Read())
                {
                    CakeName = dt.GetString("cakename");
                    ImageSource img= new BitmapImage(new Uri(@"D:\hunterZ Documents\Systems\UnderDevelopment\SweetApp\SweetApp\bin\Debug\AppX\cakeimages\keiki1.jpg", UriKind.Relative));
            CakeData.Add(new CakeData { Cakename = wew, _imageSource = img});//is it possible to apply the custom path like D:/pictures/blackforest.png?
                }
                dt.Close();

            }

这是我的xaml代码:

<page.DataContext>
<vm:CakeDataSource/>
</page.DataContext>

....
     <GridView.ItemTemplate>
                        <DataTemplate>
                            <Grid HorizontalAlignment="Left" Width="397" Height="248">
                                <Border Background="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}">
                                    <Image Source="{Binding ImagePath}" Stretch="UniformToFill"/>//this is the picture that i want to modify`
                                </Border>
                                <StackPanel VerticalAlignment="Bottom" Background="{ThemeResource ListViewItemOverlayBackgroundThemeBrush}">
                                    <TextBlock x:Name="CakeName"  Text="{Binding Cakename}" Foreground="{ThemeResource ListViewItemOverlayForegroundThemeBrush}" Style="{StaticResource TitleTextBlockStyle}" Height="60" Margin="15,0,15,0"/>

                                </StackPanel>
                            </Grid>
                        </DataTemplate>
                    </GridView.ItemTemplate>

1 个答案:

答案 0 :(得分:0)

正如我在评论中发布的那样,您应该将名为ImageSource的属性类型更改为类型 ImageSource以及:

public ImageSource ImageSource { get; set; }

然后,设置您的图像 - 例如在ViewModel的ctor中:

public MyViewModel()
{
    ImageSource = new BitmapImage(new Uri(@"path/to/your/image.jpg", UriKind.Relative));
}

路径必须相对于您的应用程序目录,否则请使用@"C:\Path\To\My\Image.jpg"之类的绝对路径,并将UriKind设置为Absolute

请注意,如果您将ImageSource设置为不在ctor中,则您的ViewModel需要实施INotifyPropertyChanged并为您的属性提升PropertyChanged

修改:检查您的装订!您的Viewmodel还提供了一个名为ImageSource的属性,您的视图绑定到ImagePath