2
votes

I'm making a program with buttons that has 2 textblocks: 1 in the left that contains the Username and one on the right that has "Begin" And these buttons are added for each user registered.

I have two projects in this solution: One (VB.NET) as the Main Program and the other (C#) as the CustomControlLibrary...

Now, I want to make this custom button in the Custom Control Library... I'm trying to create a new Custom Control and changed the ": Control" into ":Button"

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:LC_Lib.LC_Controls">


    <Style TargetType="{x:Type local:UserSelectButton}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:UserSelectButton}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="Black"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                            <TextBlock Name="txtb_user"></TextBlock>
                        </Grid>
                    </Border>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LC_Lib.LC_Controls
{
    public class UserSelectButton : Button
    {
        private string u_name = "";
        static UserSelectButton()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(UserSelectButton), new FrameworkPropertyMetadata(typeof(UserSelectButton)));
        }
        public string UserName
        {
            get { return u_name; }
            set { u_name = value; }
        }
    }
}

VB.NET

    For Each user In users_list
        Dim user_btn As New UserSelectButton

        user_btn.UserName = user.Name
        Dim top As Double = users_list.IndexOf(user) * 40 + 20
        user_btn.Margin = New Thickness(20, top, 0, 75)
        user_btn.Tag = users_list.IndexOf(user)
        AddHandler user_btn.Click, Sub(s, a)
                                       SignIn()
                                   End Sub
    Next

  • What should I do for them to appear in my form? I can't seem to see them, but when I click them, it actually fires...
  • Can I "add" two textblocks inside the Grid by inserting textblock elements in the border in the Generic.xaml?
  • If so, or the other way, how can I assign the textblock's text in the UserName property of the inherited button?
2

2 Answers

0
votes

If so, or the other way, how can I assign the textblock's text in the UserName property of the inherited button?

You can create DependencyProperty in your UserSelectButton and do a Binding to it in your XAML style with RelativeSource={RelativeSource TemplatedParent} and Mode=TwoWay. Then you'll can bind anything to that dependency property.

0
votes

There is no need to inherit the button, you are not changing its functionality? You should simply create a Style that sets the content of the button and set it to all your buttons :

 <Style TargetType="{x:Type Button}">
                    <Setter Property="Content">
                        <Setter.Value>
                            <Border Background="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Button}, Path=Background}" 
                                    BorderBrush="Black" BorderThickness="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Button}, Path=Background}">
                                <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                                    <TextBlock Name="txtb_user" Text="{Binding UserName"></TextBlock>
                                </Grid>
                            </Border>
                        </Setter.Value>
                    </Setter>
                </Style>