Soy programador C# y estoy recién utilizando WPF y quisiera replicar estos botones en xaml, empezando por su forma!
-
Bienvenido a stackoverflow en español, puede que le interese leer esto es.stackoverflow.com/tour, puede tratar de mejorar su pregunta mirando este link es.stackoverflow.com/help/how-to-ask, es posible que su pregunta sea cerrada no se lo tome a mal. Saludos– toledanoel 4 may. 2016 a las 4:05
1 respuesta
Básicamente estos controles se crean con estilos (Styles) y propiedades adjuntas (Attached Properties), en ocasiones también puedes utilizar Desencadenadores (Triggers) con el lenguaje XAML.
Por ejemplo, puedes crear un botón de la siguiente manera, mediante un archivo de recursos o en el mismo XAML de tu página:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:botoncito="clr-namespace:MiProyecto.PropiedadesBoton">
...
<Style x:Key="Imagen1" TargetType="Button">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Path=(botoncito:ButtonProperties.Image), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}"></Image>
<ContentPresenter Content="{Binding Path=Content, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}"></ContentPresenter>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Definimos las propiedades adjuntas (Attached Properties) mediante código C#:
public class PropiedadesBoton
{
public static ImageSource GetImage(DependencyObject obj)
{
return (ImageSource)obj.GetValue(ImageProperty);
}
public static void SetImage(DependencyObject obj, ImageSource value)
{
obj.SetValue(ImageProperty, value);
}
public static readonly DependencyProperty ImageProperty =
DependencyProperty.RegisterAttached("Image", typeof(ImageSource), typeof(PropiedadesBoton), new UIPropertyMetadata((ImageSource)null));
}
Por último, en tu archivo XAML, donde utilizarás el botón en sí, puedes colocar el siguiente código:
<Button Style="{StaticResource Imagen1}" botoncito:ButtonProperties.Image="{StaticResource MyImage}" Content="Test">
</Button>
El tutorial proveído por MSDN es bastante interesante y completo para aprender a realizar esta tarea. Casi todos los controles WPF pueden personalizarse.
-
Un millón de gracias, pude entender parte de tu código pero me esta dando problemas esta linea botoncito:ButtonProperties.Image="{StaticResource MyImage}" sera por el namespace de mi proyecto o algo parecido? el 5 may. 2016 a las 1:35
-
botoncito:ButtonProperties.Image="{StaticResource MyImage}" Es por el recurso, debe ser el que hayas creado como recurso en tu XAML. el 5 may. 2016 a las 12:18