Hello,
I am very new to this wpf stuff and I was trying to follow a tutorial to create a user control with dependency property using wpf framework and ran into road blocks.
I attempted to create a very simple control and this is the code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; 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 Test_2 { /// <summary> /// Interaction logic for ShowNumberControl.xaml /// </summary> public partial class ShowNumberControl : UserControl { public ShowNumberControl() { InitializeComponent(); } public int CurrentNumber { get { return (int)GetValue(CurrentNumberProperty); } set { SetValue(CurrentNumberProperty, value); } } // Using a DependencyProperty as the backing store for CurrentNumber. This enables animation, styling, binding, etc... public static readonly DependencyProperty CurrentNumberProperty = DependencyProperty.Register("CurrentNumber", typeof(int), typeof(ShowNumberControl), new UIPropertyMetadata(100), new ValidateValueCallback(ValidateCurrentNumber)); private static bool ValidateCurrentNumber(object value) { if (Convert.ToInt32(value)>=0 && Convert.ToInt32(value)<=500) { return true; } return false; } } }
<UserControl x:Class="Test_2.ShowNumberControl" 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" d:DesignHeight="300" d:DesignWidth="300"><Grid><Label x:Name="numberDisplay" Height="50" Width="200" Background="LightBlue"/></Grid></UserControl>
The xaml of the main program
<Window x:Class="Test_2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:myCtrls="clr-namespace:Test_2" Title="MainWindow" Height="150" Width="250" WindowStartupLocation="CenterScreen"><StackPanel><myCtrls:ShowNumberControl x:Name="myShowNumberCtrl" CurrentNumber="100"/> <myCtrls:ShowNumberControl.Triggers><EventTrigger RoutedEvent="myCtrls:ShowNumberControl.Loaded"><EventTrigger.Actions><BeginStoryboard><Storyboard TargetProperty="CurrentNumber"><Int32Animation From="100" To="200" Duration="0:0:10"/></Storyboard></BeginStoryboard></EventTrigger.Actions></EventTrigger></myCtrls:ShowNumberControl.Triggers></StackPanel></Window>
Basically, I was trying to create a simple animation to increment the text box. But for some reason, the compiler is saying Trigger not found or not accessible. If I comment the animation out, I still do not see the value in the text box and at some point it was also saying the dependency property CurrentNumber not available or not accessible.
It is probably some obvious thing that I have overlooked due to my inexperience with wpf. If you could point it out to me, it would be great.
Thanks,