WPF中ImageBrush边框背景的奇怪行为

时间:2017-12-15 09:35:34

标签: wpf background-image imagebrush

我有边框,2个按钮和2个jpg文件。

这是我的第一个按钮的代码:

private void Button1_Click(object sender, RoutedEventArgs e)
{
    Uri uri = new Uri(@"C:\Users\abc\Documents\t1.jpg", UriKind.Relative);
    BitmapImage bi = new BitmapImage();
    bi.BeginInit();
    bi.CacheOption = BitmapCacheOption.OnLoad; 
    bi.UriSource = uri;
    bi.EndInit();
    ImageBrush ib = new ImageBrush(bi)
    {
        Stretch = Stretch.UniformToFill,
        AlignmentX = AlignmentX.Center,
        AlignmentY = AlignmentY.Center
    };
    myBorder.Background = ib;
}

这是我的第二个按钮的代码:

private void Button2_Click(object sender, RoutedEventArgs e)
{
    File.Copy(@"C:\Users\abc\Documents\t2.jpg", @"C:\Users\abc\Documents\t1.jpg", true);
}

我在做什么:

  1. 初始状态:
  2. enter image description here

    1. 点击按钮1(设置):图片t1.jpg设置为myBorder的背景。没关系。
    2. enter image description here

      1. 单击按钮2(更改文件):文件t1.jpg的内容将替换为文件t2.jpg的内容,名称保持不变。

      2. 再次点击按钮1(设置):现在myBorder应具有不同的背景,因为文件t1.jpg的内容已替换为文件t2. jpg的内容。 但什么都没发生!为什么?

0 个答案:

没有答案