在一个按钮上连接两个内容或图像和内容,需要c#代码

时间:2017-01-12 08:44:55

标签: c# wpf

我尝试使用MainWindow源代码中的查找资源和XAML中的窗口资源将图像粘贴到多个按钮上。但问题是,图像会删除按钮内容。怎么解决? 我可以将图像粘贴到边角或此问题的任何其他解决方案吗?

的.cs

private void button_Click(object sender, Routed Event Args e) 
{ 
   Button button = (Button)sender; 
   if (isGroupOpen == true) 
   { 
      List<Button> buttonlistA = new List<Button>(); 
      buttonlistA.Add(button); 
      button.Content = FindResource("Play1");
   }
}

的.xaml

<Window.Resources> 
   <Image x:Key="Play1" x:Shared="false" Name="button" Source="image.png" Height="20" Width="20" Margin="30px,-20px,-20px,30px" HorizontalAlignment="Left" VerticalAlignment="Top"> 
   </Image> 
</Window.Resources>

1 个答案:

答案 0 :(得分:0)

我举一个例子来试验。评论时,在尝试定义WPF中某些内容的可视化表示时,您不应该依赖于代码。由于您没有显示大部分代码,因此无法为您的特定问题实例提供精确答案,但这个答案仍然可以让您了解可见的设计和功能是如何分离的。

在以下示例中,有一个初始按钮和一个复选框。当您单击该按钮时,会在列表A或列表B中生成一个新按钮(取决于CheckBox.IsChecked),并带有一个图标和一个处理点击的Command

以下假设您的项目包含图片/Images/Yes.png /Images/No.png。您可以根据需要调整实际图像属性。

代码

public class ButtonDescription
{
    public object ButtonContent { get; set; }

    public ICommand ButtonCommand { get; set; }
}

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        Loaded += MainWindow_Loaded;
    }

    public ObservableCollection<ButtonDescription> ButtonListA { get; set; }
    public ObservableCollection<ButtonDescription> ButtonListB { get; set; }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        ButtonListA = new ObservableCollection<ButtonDescription>();
        ButtonListB = new ObservableCollection<ButtonDescription>();
        grid1.DataContext = this;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Button button = (Button)sender;
        if (TargetDescriptor.IsChecked == true)
        {
            ButtonListA.Add(new ButtonDescription { ButtonContent = button.Content, ButtonCommand = ApplicationCommands.Open });
        }
        else
        {
            ButtonListB.Add(new ButtonDescription { ButtonContent = button.Content, ButtonCommand = ApplicationCommands.Close });
        }
    }

    private void Open_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        MessageBox.Show("Open");
    }
    private void Close_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        MessageBox.Show("Close");
    }
}

xaml

<Window x:Class="WpfTests.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Window.CommandBindings>
        <CommandBinding Command="ApplicationCommands.Open" Executed="Open_Executed"/>
        <CommandBinding Command="ApplicationCommands.Close" Executed="Close_Executed"/>
    </Window.CommandBindings>
    <Window.Resources>
        <!-- Button content templates with image before text content -->
        <DataTemplate x:Key="buttonContentA">
            <StackPanel Orientation="Horizontal">
                <Image Source="/Images/Yes.png" Margin="3" Width="16" Height="16"/>
                <ContentPresenter Content="{Binding}" VerticalAlignment="Center"/>
            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Key="buttonContentB">
            <StackPanel Orientation="Horizontal">
                <Image Source="/Images/No.png" Margin="3" Width="16" Height="16"/>
                <ContentPresenter Content="{Binding}" VerticalAlignment="Center"/>
            </StackPanel>
        </DataTemplate>

        <!-- Common style -->
        <Style  x:Key="buttonStyle" TargetType="{x:Type Button}">
            <Setter Property="Margin" Value="5 3"/>
        </Style>

        <!-- Specific style for A -->
        <Style x:Key="buttonStyleA" TargetType="{x:Type Button}" BasedOn="{StaticResource buttonStyle}">
            <Setter Property="ContentTemplate" Value="{StaticResource buttonContentA}"/>
            <Setter Property="Command" Value="ApplicationCommands.Open"/>
        </Style>

        <!-- Specific style for B -->
        <Style x:Key="buttonStyleB" TargetType="{x:Type Button}" BasedOn="{StaticResource buttonStyle}">
            <Setter Property="ContentTemplate" Value="{StaticResource buttonContentB}"/>
            <Setter Property="Command" Value="ApplicationCommands.Close"/>
        </Style>

        <!-- Let the list items of type ButtonDescription be shown as Button -->
        <DataTemplate x:Key="ButtonsListItemTemplate">
            <Button Content="{Binding ButtonContent}"/>
        </DataTemplate>
    </Window.Resources>
    <Grid x:Name="grid1">
        <StackPanel>
            <CheckBox x:Name="TargetDescriptor" Content="Copy to List A" IsChecked="True" Margin="3 5"/>
            <Button Content="Click me please" Click="Button_Click" Margin="3 5" MinHeight="28"/>

            <TextBlock FontWeight="Bold" Text="List A" Margin="3 5"/>
            <ItemsControl ItemsSource="{Binding ButtonListA}" ItemTemplate="{StaticResource ButtonsListItemTemplate}">
                <ItemsControl.Resources>
                    <Style TargetType="{x:Type Button}" BasedOn="{StaticResource buttonStyleA}"/>
                </ItemsControl.Resources>
            </ItemsControl>

            <TextBlock FontWeight="Bold" Text="List B" Margin="3 5"/>
            <ItemsControl ItemsSource="{Binding ButtonListB}" ItemTemplate="{StaticResource ButtonsListItemTemplate}">
                <ItemsControl.Resources>
                    <Style TargetType="{x:Type Button}" BasedOn="{StaticResource buttonStyleB}"/>
                </ItemsControl.Resources>
            </ItemsControl>
        </StackPanel>
    </Grid>
</Window>