Binding image value to path in wpf and c#

后端 未结 1 679
野趣味
野趣味 2021-01-27 07:08

I am strugling to bind value to path ImageSource. I get NullReferenceError when I try to set new value. My current code is:

In MainWindow.xaml the Path code is following

相关标签:
1条回答
  • 2021-01-27 07:30

    In code behind you have to use complete Pack URIs, including the pack://application:,,, prefix. And you should not specify UriKind.Relative.

    private void Path_MouseEnter_1(object sender, MouseEventArgs e)
    {
       image.ImageSource = new Uri("pack://application:,,,/RoundUI;component/sadface.png");
    }
    

    Edit: It seems that you are confusing a few things here. For now it might perhaps be easier not to bind the ImageBrush's ImageSource property, but instead set it directly in code behind, as shown in this answer to your previous question:

    <Path x:Name="PPButton" ...
          MouseEnter="Path_MouseEnter_1" MouseLeave="Path_MouseLeave_1" >
        <Path.Fill>
            <ImageBrush/>
        </Path.Fill>
    </Path>
    

    Code behind:

    private void Path_MouseEnter_1(object sender, MouseEventArgs e)
    {
       var imageBrush = PPButton.Fill as ImageBrush;
       image.ImageSource = new Uri("pack://application:,,,/RoundUI;component/sadface.png");
    }
    
    0 讨论(0)
提交回复
热议问题