Custom Button with Binding

I would like to create a Custon Button in WPF, so I have write this code:

<UserControl x:Class="RiabilitazioneCognitiva.ButtonPersonalizzati"
             DataContext="{Binding RelativeSource={RelativeSource Self}}"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" Width="Auto" Height="Auto">
    <FrameworkElement.Resources>
        <ResourceDictionary Source="GlassButton.xaml" />
    </FrameworkElement.Resources>
        <Button  x:Name="pippo" Style="{DynamicResource GlassButton}" 
        Click="button_Click">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding Text}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="#FFFFFFFF"  />

        </StackPanel>
    </Button>


</UserControl>

Now i insert this Button in my page, so i try this code:

<Window xmlns:RiabilitazioneCognitiva="clr-namespace:RiabilitazioneCognitiva"  x:Name="framePrincipale" x:Class="RiabilitazioneCognitiva.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:RiabilitazioneCognitiva"
        Title="Stop" 
        Height="{Binding}" 
        Width="{Binding}" 
        Background="White" 
        WindowStartupLocation="CenterScreen" 
        WindowStyle="None"
        WindowState="Maximized"
        ResizeMode="NoResize">
</Window>

<Grid>
   <local:ButtonPersonalizzati />
</Grid>

It found, but if i insert this i don't see Button

<local:ButtonPersonalizzati x:Text="pp" >

Can we help me?

Thanks

PS: in ButtonPersonalizzati.cs i have this

public string Text { 
   get{return Text;}
}

Answers


You haven't set your data context. There are a variety of ways to deal with this, but my typical approach is to do something like this:

<UserControl ...
             x:Name="ucThis">
         ...
        <TextBlock Text="{Binding ElementName=ucThis Path=Text}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="#FFFFFFFF"  />
         ...
</UserControl>

Close the tag local:ButtonPersonalizzati

<local:ButtonPersonalizzati x:Name="pp" local:Text="Pippo" />

Define Text as a DependencyProperty so you can bind to it:

public string Text
{
    get { return (string)GetValue(TextProperty); }
    set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty TextProperty =
    DependencyProperty.Register("Text", typeof(string), typeof(ButtonPersonalizzati), new UIPropertyMetadata(""));

You should also set the DataContext in the constructor of your class:

this.DataContext=this;

Need Your Help

Job queueing and execute Mechanism

java spring

In my webservice all method calls submits jobs to a queue. Basically these operations take long time to execute, so all these operations submit a Job to a queue and return a status saying "Submitte...

Copy multiple files in Python

python file copy

How to copy all the files present in one directory to another directory using Python.