반응형
이미지 포함 WPF 버튼
WPF의 버튼에 이미지를 첨부하려고 하는데 이 코드가 실패합니다.비슷한 코드가 Mozilla XUL에서 완벽하게 작동한다면 이상할 것 같습니다.
<Button Height="49.086" Margin="3.636,12,231.795,0" Name="button2"
VerticalAlignment="Top" Grid.Column="1" Click="button2_Click"
Source="Pictures/apple.jpg">Disconnect from Server</Button>
대신 다음과 같은 작업을 수행해야 합니다.
<Button>
<StackPanel>
<Image Source="Pictures/apple.jpg" />
<TextBlock>Disconnect from Server</TextBlock>
</StackPanel>
</Button>
이미지를 풀버튼으로 스트레치 하는 다른 방법.아래 코드를 사용해 보십시오.
<Grid.Resources>
<ImageBrush x:Key="AddButtonImageBrush" ImageSource="/Demoapp;component/Resources/AddButton.png" Stretch="UniformToFill"/>
</Grid.Resources>
<Button Content="Load Inventory 1" Background="{StaticResource AddButtonImageBrush}"/>
여기에서 참조
다른 사람에게도 도움이 될 수 있습니다.여기 MouseOver Option에도 같은 글을 올렸습니다.
가장 간단한 방법은 이미지 태그를 사용하는 것입니다.
<Button Name="btn" Width="26" Height="26" Click="btnClick">
<Image Source="Resource/btn-icon.png"/>
</Button>
이미지 파일이 리소스 폴더에 추가되었다고 가정합니다.
<Button x:Name="myBtn_DetailsTab_Save" FlowDirection="LeftToRight" HorizontalAlignment="Left" Margin="835,544,0,0" VerticalAlignment="Top" Width="143" Height="53" BorderBrush="#FF0F6287" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" FontFamily="B Titr" FontSize="15" FontWeight="Bold" BorderThickness="2" Click="myBtn_DetailsTab_Save_Click">
<StackPanel HorizontalAlignment="Stretch" Background="#FF1FB3F5" Cursor="Hand" >
<Image HorizontalAlignment="Left" Source="image/bg/Save.png" Height="36" Width="124" />
<TextBlock HorizontalAlignment="Center" Width="84" Height="22" VerticalAlignment="Top" Margin="0,-31,-58,0" Text="ثبت مشتری" />
</StackPanel>
</Button>
이거면 될 거 아냐?
<Button Content="Test">
<Button.Background>
<ImageBrush ImageSource="folder/file.PNG"/>
</Button.Background>
</Button>
Button 클래스에서 상속하는 사용자 정의 컨트롤을 만들 수 있습니다.이 코드는 보다 재사용이 용이합니다.자세한 내용은 다음 블로그 투고를 참조해 주십시오.WPF - create custom button with image ( Image Button )
이 컨트롤을 사용하여:
<local:ImageButton Width="200" Height="50" Content="Click Me!"
ImageSource="ok.png" ImageLocation="Left" ImageWidth="20" ImageHeight="25" />
ImageButton.cs 파일:
public class ImageButton : Button
{
static ImageButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ImageButton), new FrameworkPropertyMetadata(typeof(ImageButton)));
}
public ImageButton()
{
this.SetCurrentValue(ImageButton.ImageLocationProperty, WpfImageButton.ImageLocation.Left);
}
public int ImageWidth
{
get { return (int)GetValue(ImageWidthProperty); }
set { SetValue(ImageWidthProperty, value); }
}
public static readonly DependencyProperty ImageWidthProperty =
DependencyProperty.Register("ImageWidth", typeof(int), typeof(ImageButton), new PropertyMetadata(30));
public int ImageHeight
{
get { return (int)GetValue(ImageHeightProperty); }
set { SetValue(ImageHeightProperty, value); }
}
public static readonly DependencyProperty ImageHeightProperty =
DependencyProperty.Register("ImageHeight", typeof(int), typeof(ImageButton), new PropertyMetadata(30));
public ImageLocation? ImageLocation
{
get { return (ImageLocation)GetValue(ImageLocationProperty); }
set { SetValue(ImageLocationProperty, value); }
}
public static readonly DependencyProperty ImageLocationProperty =
DependencyProperty.Register("ImageLocation", typeof(ImageLocation?), typeof(ImageButton), new PropertyMetadata(null, PropertyChangedCallback));
private static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var imageButton = (ImageButton)d;
var newLocation = (ImageLocation?) e.NewValue ?? WpfImageButton.ImageLocation.Left;
switch (newLocation)
{
case WpfImageButton.ImageLocation.Left:
imageButton.SetCurrentValue(ImageButton.RowIndexProperty, 1);
imageButton.SetCurrentValue(ImageButton.ColumnIndexProperty, 0);
break;
case WpfImageButton.ImageLocation.Top:
imageButton.SetCurrentValue(ImageButton.RowIndexProperty, 0);
imageButton.SetCurrentValue(ImageButton.ColumnIndexProperty, 1);
break;
case WpfImageButton.ImageLocation.Right:
imageButton.SetCurrentValue(ImageButton.RowIndexProperty, 1);
imageButton.SetCurrentValue(ImageButton.ColumnIndexProperty, 2);
break;
case WpfImageButton.ImageLocation.Bottom:
imageButton.SetCurrentValue(ImageButton.RowIndexProperty, 2);
imageButton.SetCurrentValue(ImageButton.ColumnIndexProperty, 1);
break;
case WpfImageButton.ImageLocation.Center:
imageButton.SetCurrentValue(ImageButton.RowIndexProperty, 1);
imageButton.SetCurrentValue(ImageButton.ColumnIndexProperty, 1);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
public ImageSource ImageSource
{
get { return (ImageSource)GetValue(ImageSourceProperty); }
set { SetValue(ImageSourceProperty, value); }
}
public static readonly DependencyProperty ImageSourceProperty =
DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(ImageButton), new PropertyMetadata(null));
public int RowIndex
{
get { return (int)GetValue(RowIndexProperty); }
set { SetValue(RowIndexProperty, value); }
}
public static readonly DependencyProperty RowIndexProperty =
DependencyProperty.Register("RowIndex", typeof(int), typeof(ImageButton), new PropertyMetadata(0));
public int ColumnIndex
{
get { return (int)GetValue(ColumnIndexProperty); }
set { SetValue(ColumnIndexProperty, value); }
}
public static readonly DependencyProperty ColumnIndexProperty =
DependencyProperty.Register("ColumnIndex", typeof(int), typeof(ImageButton), new PropertyMetadata(0));
}
public enum ImageLocation
{
Left,
Top,
Right,
Bottom,
Center
}
Generic.xaml 파일:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfImageButton">
<Style TargetType="{x:Type local:ImageButton}" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Image Source="{Binding ImageSource, RelativeSource={RelativeSource AncestorType=local:ImageButton}}"
Width="{Binding ImageWidth, RelativeSource={RelativeSource AncestorType=local:ImageButton}}"
Height="{Binding ImageHeight, RelativeSource={RelativeSource AncestorType=local:ImageButton}}"
Grid.Row="{Binding RowIndex, RelativeSource={RelativeSource AncestorType=local:ImageButton}}"
Grid.Column="{Binding ColumnIndex, RelativeSource={RelativeSource AncestorType=local:ImageButton}}"
VerticalAlignment="Center" HorizontalAlignment="Center"></Image>
<ContentPresenter Grid.Row="1" Grid.Column="1" Content="{TemplateBinding Content}"
VerticalAlignment="Center" HorizontalAlignment="Center"></ContentPresenter>
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
언급URL : https://stackoverflow.com/questions/2697383/wpf-button-with-image
반응형
'programing' 카테고리의 다른 글
| 셸 스크립트 내에서 INI 값을 취득하려면 어떻게 해야 합니까? (0) | 2023.04.19 |
|---|---|
| Excel - 값 변경에 따라 행 전체를 음영 처리 (0) | 2023.04.19 |
| 실행 중인 화면 세션을 나열하려면 어떻게 해야 합니까? (0) | 2023.04.19 |
| 부울 리스트에서 True 값의 인덱스 가져오기 (0) | 2023.04.19 |
| 도커 - 캐시 키 계산 실패: 찾을 수 없음 - Visual Studio에서 정상적으로 실행됨 (0) | 2023.04.19 |