Wednesday, March 14, 2018

Please help me do this correctly...

So I'm attempting to create my own custom control. I got through the process but realized I was unable to used the "IsPressed" trigger as the user control wasn't technically a button. So I did some research to figure out how to inherit the "IsPressed" condition from a button on my user control but that didn't pan out. The best answer I could find involved switching from creating a custom user control to a custom button. I did this but it required me to template the button in order to get the look I wanted. Unfortunately, this hindered my ability (with my knowledge level, at least) to assign properties to the items within the button. So, I went a super hacky route and was able to get what I was looking for but not without it feeling gross to my eye balls.

Basically, I created holder controls within the button, bound those controls to a property, then bound the controls within the control template to the holder controls. This allowed me to see the property on my application and linking everything together.

I'm not sure how to handle this correctly and I'm hoping someone here will be able to help me figure out the correct way to create this custom button of mine without causing a good developer to throw up when they look at my code.

Here are some code snippets:

Custom Control XAML:

<Button x:Class="Tool_Inventory_and_Calibration_Manager.Views.Controls.NavBarButton" 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" xmlns:local="clr-namespace:Tool_Inventory_and_Calibration_Manager.Views.Controls" mc:Ignorable="d" d:DesignWidth="150" d:DesignHeight="30"> <Button.Style> <Style TargetType="{x:Type Button}"> <Setter Property="Background" Value="#202020"/> <Setter Property="BorderBrush" Value="#000000"/> <Setter Property="BorderThickness" Value="1"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="auto"/> <ColumnDefinition/> </Grid.ColumnDefinitions> <Image Grid.Column="0" Width="16" Height="16" RenderOptions.BitmapScalingMode="HighQuality" SnapsToDevicePixels="True" Source="{Binding ElementName=Icon, Path=Source}"/> </Grid> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </Button.Style> <Grid> <Image x:Name="Icon"/> </Grid> </Button> 

Custom Control C#:

public partial class NavBarButton : Button { public NavBarButton() { InitializeComponent(); } public ImageSource IconSource { get { return Icon.Source; } set { Icon.Source = value; } } } 

ShellView XAML:

<Window x:Class="Tool_Inventory_and_Calibration_Manager.Views.ShellView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Tool_Inventory_and_Calibration_Manager.Views" xmlns:controls="clr-namespace:Tool_Inventory_and_Calibration_Manager.Views.Controls" mc:Ignorable="d" Style="{DynamicResource DefaultWindow}"> <Window.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Styles/ViewStyles.xaml"/> </ResourceDictionary.MergedDictionaries> <BooleanToVisibilityConverter x:Key="btv"/> </ResourceDictionary> </Window.Resources> <Grid Margin="6"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" MinWidth="150"/> <ColumnDefinition Width="10*"/> </Grid.ColumnDefinitions> <Border x:Name="NavBar"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="30"/> </Grid.RowDefinitions> <controls:NavBarButton IconSource="SourceGoesHere"/> </Grid> </Border> </Grid> </Window> 
Please help me do this correctly... Click here
  • Blogger Comment
  • Facebook Comment

0 comments:

Post a Comment

The webdev Team