图像源中的WPF动态数据绑定

时间:2014-11-11 12:39:05

标签: c# wpf dynamic

我做错了什么?

的Xaml:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="26*"/>
        <RowDefinition Height="63*"/>
        <RowDefinition Height="67*"/>
    </Grid.RowDefinitions>
    <TextBlock Name ="title" Text="" TextWrapping="NoWrap" Grid.Row="0" Margin="25,10,25,0"/>
    <Image Source="{Binding Path=BindImgURL}" HorizontalAlignment="Left" Height="164" Grid.Row="1" VerticalAlignment="Top" Width="306" Margin="152,0,0,0"/>
</Grid>

C#代码:

private string Id;
    private string ImgURL;
    public VideoDetails(string Id)
    {
        InitializeComponent();
        this.Id = Id;
        DetailsGenerator dg = new DetailsGenerator(Id);
        this.ImgURL = "Dynamic source";
        this.DataContext = BindImgURL;
        MessageBox.Show(BindImgURL);

    }
    public string BindImgURL
    {
        get { return ImgURL; }
        set
        { ImgURL = value; }
    }

显示来源正确! 由MessageBox.But检查没有图像 我做错了什么?我试过删除&#34; path =&#34;但是没有工作

1 个答案:

答案 0 :(得分:1)

我想你要么将DataContext设置为this

this.DataContext = this;

或更改绑定到当前DataContext

<Image Source="{Binding}" .../>

目前Image会在当前BindImgURL中搜索DataContext,该BindImgURL设置为stringPropertyChanged

与您的问题无关,但我建议您在设置BindImgURL之后,考虑实施INotifyPropertyChanged并为DataContext举办BindImgURL次活动UI

不会对{{1}}进行任何更改