首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Binding BitmapImage UriSource

Binding BitmapImage UriSource
EN

Stack Overflow用户
提问于 2015-04-20 06:35:40
回答 3查看 2.7K关注 0票数 1

我正在尝试开发我的第一个Windows应用商店应用程序。我正在使用集线器应用程序模板。我想在我的HubPage的第一部分显示一个来自Url的图像:

代码语言:javascript
复制
<HubSection ... >
    <DataTemplate>
        <Grid ... >
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                ...
            </Grid.RowDefinitions>

            <Image Name="UserProfileImage" Margin="100, 0, 100, 0" Grid.Row="0" VerticalAlignment="Top">
                <Image.Source>
                    <BitmapImage UriSource="{Binding ImageUrl}"></BitmapImage>
                </Image.Source>
            </Image>
        </Grid>
    </DataTemplate>
</HubSection>

在我的HubPage.xaml.cs中:

代码语言:javascript
复制
[DefaultValue("http://images6.fanpop.com/image/photos/34200000/more-dumb-images-of-philip-j-fry-futurama-34257101-1440-900.png"")]
public string ImageUrl { get; set; }

但没有显示任何内容。如果我在xaml文件中手动设置一个图像url,它工作得很好…

EN

回答 3

Stack Overflow用户

发布于 2015-04-20 07:11:30

问题是,绑定机制不知道在哪里查找ImageUrl属性。您可以将标记的DataSource或其任何父级设置为类的实例,属性就是在该实例中定义的。

或者在每个绑定语句中使用更多信息。要绑定到您自己,只需使用

代码语言:javascript
复制
UriSource="{Binding RelativeSource={RelativeSource Self}, Path=ImageUrl}"

代码语言:javascript
复制
UriSource="{Binding ImageUrl, RelativeSource={RelativeSource Self}}"

另请参阅WPF Bind to itself

编辑:

您还需要在绑定到的变量上有一个通知机制。将其设置为DependencyProperty或使用PropertyChanged event (通过INotifyPropertyChanged并使用属性名称在setter中的更改时调用PropertyChanged ),或者创建名为<name of property>Changed的事件并在更改时调用此事件。

票数 1
EN

Stack Overflow用户

发布于 2017-02-14 12:59:24

不幸的是,BitmapSource.UriSource似乎不能进行数据绑定(或者更具体地说,它只能绑定一次,并且忽略进一步的更改)。请参阅讨论here

票数 1
EN

Stack Overflow用户

发布于 2015-04-20 15:09:16

在您的代码中,您使用的是<DataTemplate>,这意味着它的父控件将是<ListView>或类似的控件。

你的代码很少反映你的场景。我建议你绑定ImageSource而不是string

我建议您编辑代码隐藏部分,而不是处理XAML部分。

我在这里向样本做简报。将这一点与你的情况联系起来,并做一些必要的事情。

示例:-

代码语言:javascript
复制
    // BlankPage.xaml ----
    // eg. I've a listView and i want to display
    // image dynamically in DataTemplate

    <ListView x:Name="lstView">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Image Source="{Binding bmp}" />
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

现在,定义一个model类来为ListView提供itemsSource

代码语言:javascript
复制
// In BlankPage.xaml.cs file

public class model
{
    public ImageSource bmp { get; set; }
}

现在,假设我在Page_Loaded事件中将itemsSource赋值给ListView

代码语言:javascript
复制
    // in BlankPage.xaml.cs file
    // declare this BlankPage's Loaded event in BlankPage's Constructor
    // and define the event handler like this

    void BlankPage2_Loaded(object sender, RoutedEventArgs e)
    {
        model m1 = new model()
        {
            bmp = new BitmapImage(new Uri("ms-appx:///Assets/TechVista(300x300).png", UriKind.Absolute))
        };

    // here "ms-appx:///Assets/TechVista(300x300).png" should be the Full-System-Path where image is located.
    // and according to that declare the UriKind as Relative or Absolute or other.

        List<model> lstModels = new List<model>();
        lstModels.Add(m1);

    // you can add as many models as you want here.
    // for reference I'm adding only one here.

        lstView.ItemsSource = lstModels;
    }

它肯定会工作的。要获得更准确的答案,请在此处详细说明。

希望这能帮上忙!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29736676

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档