Reputation: 191
I have simple Button UserControl like:
<?xml version="1.0" encoding="utf-8"?>
<UserControl
x:Class="TestProject.Controls.Button"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TestProject.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Button x:Name="MyButton" Click="Btn_Click">Aloha!</Button>
</UserControl>
/Controls/Button.xaml.cs:
namespace TestProject.Controls
{
public sealed partial class Button : UserControl
{
private void Btn_Click(object sender, EventArgs args)
{
MyButton.Background = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));
}
}
}
Insert it in the main page:
<?xml version="1.0" encoding="utf-8"?>
<Page
x:Class="TestProject.Views.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TestProject.Views"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:mycontrols="using:TestProject.Controls">
<StackPanel>
<mycontrols:Button />
</StackPanel>
</Page>
if in Btn_Click
I cast var button = (Microsoft.Ui.Xaml.Controls.Button)sender
there is exception fires says, that can't cast TestProject.Controls.Button to Microsoft.Ui.Xaml.Controls.Button. It seems that inside the UserControl even if user press button the sender is not the button anyway, but that UserControl.
Now to change the button's background I need call it by name (MyButton), but my original intention is that the UserControl should be the Microsoft.Ui.Xaml.Controls.Button. Is there any way to cast it to the Microsoft.Ui.Xaml.Controls.Button type, so by specifying this.Background =
I can change background color of the button and also make my UserControl has original Microsoft.Ui.Xaml.Controls.Button's Click event so I can specify it in Xaml like <mycontrols:Button Click="Test_Click" />
to handle click in the page I where inserted it?
Upvotes: 0
Views: 232
Reputation: 16689
Your assumption is wrong. The reason the cast failed is because you named your custom user control Button
, so any local references to Button
type in your code will assume that you mean the local class.
In this case you will either need to create a using
alias or use the fully qualified namespace to reference the correct Button
class when you try to cast the control.
(sender as Microsoft.UI.Xaml.Controls.Button).Background =
new SolidColorBrush(Color.FromArgb(255, 255, 60, 90));
It is not a good idea to create your own custom controls with names that are identical to the standard framework controls. The namespace issue will cause issues for many developers.
A standard practise is to prefix your controls with your brand or an acronym for your library. Something like MatButton
for My Awesome Tools button...
If you wanted to bind your button to the user control background then the simplest syntax is to use x:Bind directive as this does not bind to the DataContext
instead it binds to this
.
<Button x:Name="MyButton"
Click="Btn_Click"
Background="{x:Bind Background, Mode=OneWay}">Aloha!</Button>
this.Background = new SolidColorBrush(Color.FromArgb(255, 133, 66, 120));
Upvotes: 0
Reputation: 13666
I confirmed that this works:
private void Btn_Click(object sender, RoutedEventArgs e)
{
// Since you named your `UserControl` "Button",
// you need to add the Microsoft.UI.Xaml.Controls namespace.
if (sender is not Microsoft.UI.Xaml.Controls.Button button)
{
return;
}
button.Resources["ButtonBackgroundPointerOver"] = new SolidColorBrush(Colors.Green);
button.Resources["ButtonBackgroundPressed"] = new SolidColorBrush(Colors.Blue);
// "ButtonBackground" doesn't work
// because it's not set when the VisualState changes.
//button.Resources["ButtonBackground"] = new SolidColorBrush(Colors.Red);
button.Background = new SolidColorBrush(Colors.Red);
}
You can see it in the generic.xaml by searching for "DefaultButtonStyle".
Upvotes: 1