【发布时间】:2014-02-19 09:57:42
【问题描述】:
我正在开发一个购物目录,我是 MVVM 的菜鸟,所以请帮助我,是否可以在 ImageSource 中绑定自定义路径?这是我的基本代码:
景色?
public class CakeData
{
public string Cakename { get; set; }
public ImageSource _imageSource { get; set; }
}
还有数据源:
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>
【问题讨论】:
-
我尝试直接绑定它,但出现空白图像。我认为可以使用解决方案中引用的图像(例如:资产)
-
请向我们展示您的 xaml 文件。无论如何,看看:stackoverflow.com/a/2574144/3257426。我还假设,您绑定到没有 ValueConverter 的属性?因此,您的输入错误 - ImageSource 不应为
string,而应为ImageSource类型。 -
意思是我将ImageSource变量声明为图像的“ImageSource”?
-
请向我们展示您的 XAML 代码..
-
我已经包含了我想要修改的图像的 xaml 代码。请保持答案尽可能简单,因为我是 mvvm 的菜鸟。我想一步一步清楚地理解它们。
标签: c# windows mvvm windows-runtime winrt-xaml