在 WPF 中,我可以在 2 个按钮之间共享相同的图像资源吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14110068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:51:13  来源:igfitidea点击:

In WPF, can I share the same image resource between 2 buttons

c#wpfxamlresourcedictionarybitmapsource

提问by Tamar Cohen

I want to create a On/Off button in WPF and I want it to change its appearance when the user clicks it (if it was on switch to off, if it wad off switch to on) using images. I added the images I want to use to the resources:

我想在 WPF 中创建一个开/关按钮,并且我希望它在用户单击它时更改其外观(如果它打开,则切换到关闭,如果它关闭,则切换到打开)使用图像。我将要使用的图像添加到资源中:

 <Window.Resources>
    <Image x:Key="Off1" Source="/WPFApplication;component/Images/off_button.png" Height="30" Width="70" />
    <Image x:Key="On1" Source="/WPFApplication;component/Images/on_button.png" Height="30" Width="70"/>
 </Window.Resources>

And the event code is, "flag" is a Boolean local variable initialize as true:

事件代码是,“flag”是一个布尔局部变量,初始化为真:

 private void OnOff1Btn_Click(object sender, RoutedEventArgs e)
    {
        if (flag)
        {
            OnOff1Btn.Content = FindResource("Off1");
            flag = false;     
        }
        else
        {
            OnOff1Btn.Content = FindResource("On1");
            flag  = true;
        }
    }

Now I need to create 2 on/off buttons, that behave the same. When I tried to use the same resources for the second button I got an exception:

现在我需要创建 2 个开/关按钮,它们的行为相同。当我尝试对第二个按钮使用相同的资源时,出现异常:

 Specified element is already the logical child of another element. Disconnect it first.

Can I use the same images resources in the second button or do I have to add the images again as resources with different Key?

我可以在第二个按钮中使用相同的图像资源,还是必须再次将图像添加为具有不同 Key 的资源?

回答by Artiom

Set Shared in your style to false

将您的风格中的 Shared 设置为 false

<StackPanel >
   <StackPanel.Resources>
      <Image x:Key="flag" Source="flag-italy-icon.png" Width="10" x:Shared="false"/>
   </StackPanel.Resources>

   <ContentControl Content="{DynamicResource flag}" />
   <ContentControl Content="{DynamicResource flag}" />

回答by Tilak

You should use BitmapImagefor image sharing.

您应该使用BitmapImage进行图像共享。

<BitmapImage x:Key="Off1" UriSource="/WPFApplication;component/Images/off_button.png" Height="30" Width="70" />
<BitmapImage x:Key="On1" UriSource="/WPFApplication;component/Images/on_button.png" Height="30" Width="70"/>

After that you can create Multiple Imagewith BitmapImage

之后,您可以使用 BitmapImage创建多个图像

In XAML

在 XAML 中

 <Button ..>
  <Button.Content>
   <Image Source="{StaticResource Off1}" />
  </Button.Content>
 </Button>

In Code

在代码中

  Image image = new Image();
  image.Source = FindResource("Off1");
  OnOff1Btn.Content = image; 

回答by Maverik

While @Tilak's solution is definitely one way to go, you can also do this via Style.Triggers

虽然@Tilak 的解决方案绝对是一种方法,但您也可以通过 Style.Triggers

Here's an example (with the assumption that Flagis a public property exposing flag):

这是一个示例(假设Flag是公共属性公开标志):

<Button Content="{StaticResource On1}">
    <Button.Style>
        <Style>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Flag}" Value="false">
                    <Setter Property="Content" Value="{StaticResource Off1}" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>