So I have three last issues, thanks for all the help
1) if I use the usercontrol name "Name="WaitTickerCustomControl" then the Custom Control disappears in the Main Window odd.
2) The directly Bound dependency property are bound but nothing updates
3) The Indirectly Bound property's only affect the third ticker odd...
Thanks for the help im learning alot.
Madaxe
XAML Custom Control
<pre><UserControl x:Class="Testing.CustomControls.WaitTicker_CustomControl"
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:Testing.CustomControls"
mc:Ignorable="d"
d:DesignHeight="70" d:DesignWidth="60"
Name="WaitTickerCustomControl">
<Grid Name="WaitTickerMainGrid" Visibility="{Binding Path=ControlHideShow,ElementName=WaitTickerCustomControl, FallbackValue=Visible}">
<Border Name="Pulse1"
CornerRadius="{Binding Path=ControlCornerRadius1,ElementName=WaitTickerCustomControl,FallbackValue=10}"
Height="{Binding Path=PulseHeight1,ElementName=WaitTickerCustomControl,FallbackValue=50}"
Width="{Binding Path=PulseWidth1,ElementName=WaitTickerCustomControl,FallbackValue=10}"
Margin="10,10,0,0"
Background="{Binding Path=ControlBackground1,ElementName=WaitTickerCustomControl,FallbackValue=#4287f5}"
HorizontalAlignment="Left" VerticalAlignment="Top"/>
<Border Name="Pulse2"
CornerRadius="{Binding Path=ControlCornerRadius2,ElementName=WaitTickerCustomControl,FallbackValue=10}"
Height="{Binding Path=PulseHeight2,ElementName=WaitTickerCustomControl,FallbackValue=50}"
Width="{Binding Path=PulseWidth2,ElementName=WaitTickerCustomControl,FallbackValue=10}"
Margin="25,10,0,0"
Background="{Binding Path=ControlBackground2,ElementName=WaitTickerCustomControl,FallbackValue=#4287f5}"
HorizontalAlignment="Left" VerticalAlignment="Top"/>
<Border Name="Pulse3"
CornerRadius="{Binding Path=ControlCornerRadius3,ElementName=WaitTickerCustomControl,FallbackValue=10}"
Height="{Binding Path=PulseHeight3,ElementName=WaitTickerCustomControl,FallbackValue=50}"
Width="{Binding Path=PulseWidth3,ElementName=WaitTickerCustomControl,FallbackValue=10}"
Margin="40,10,0,0"
Background="{Binding Path=ControlBackground3,ElementName=WaitTickerCustomControl,FallbackValue=#4287f5}"
HorizontalAlignment="Left" VerticalAlignment="Top"/>
</Grid>
</UserControl>
XAML Code Behind
public partial class WaitTicker_CustomControl : UserControl, INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public Visibility ControlHideShow
{
get { return (Visibility)GetValue(ControlHideShowProperty); }
set { SetValue(ControlHideShowProperty, value); }
}
public static readonly DependencyProperty ControlHideShowProperty =
DependencyProperty.Register("ControlHideShow", typeof(Visibility),
typeof(WaitTicker_CustomControl),
new PropertyMetadata(Visibility.Hidden));
public int ControlCornerRadius1
{
get { return (int)GetValue(ControlCornerRadius1Property); }
set { SetValue(ControlCornerRadius1Property, value); }
}
public static readonly DependencyProperty ControlCornerRadius1Property =
DependencyProperty.Register("ControlCornerRadius1", typeof(int),
typeof(WaitTicker_CustomControl),
new PropertyMetadata(10));
public int ControlCornerRadius2
{
get { return (int)GetValue(ControlCornerRadius2Property); }
set { SetValue(ControlCornerRadius2Property, value); }
}
public static readonly DependencyProperty ControlCornerRadius2Property =
DependencyProperty.Register("ControlCornerRadius2", typeof(int),
typeof(WaitTicker_CustomControl),
new PropertyMetadata(10));
public int ControlCornerRadius3
{
get { return (int)GetValue(ControlCornerRadius3Property); }
set { SetValue(ControlCornerRadius3Property, value); }
}
public static readonly DependencyProperty ControlCornerRadius3Property =
DependencyProperty.Register("ControlCornerRadius3", typeof(int),
typeof(WaitTicker_CustomControl),
new PropertyMetadata(10));
public int PulseHeight1
{
get { return (int)GetValue(PulseHeight1Property); }
set { SetValue(PulseHeight1Property, value); }
}
public static readonly DependencyProperty PulseHeight1Property =
DependencyProperty.Register("PulseHeight1", typeof(int),
typeof(WaitTicker_CustomControl),
new PropertyMetadata(1));
public int PulseHeight2
{
get { return (int)GetValue(PulseHeight2Property); }
set { SetValue(PulseHeight2Property, value); }
}
public static readonly DependencyProperty PulseHeight2Property =
DependencyProperty.Register("PulseHeight2", typeof(int),
typeof(WaitTicker_CustomControl),
new PropertyMetadata(25));
public int PulseHeight3
{
get { return (int)GetValue(PulseHeight3Property); }
set { SetValue(PulseHeight3Property, value); }
}
public static readonly DependencyProperty PulseHeight3Property =
DependencyProperty.Register("PulseHeight3", typeof(int),
typeof(WaitTicker_CustomControl),
new PropertyMetadata(50));
public int PulseWidth1
{
get { return (int)GetValue(PulseWidth1Property); }
set { SetValue(PulseWidth1Property, value); }
}
public static readonly DependencyProperty PulseWidth1Property =
DependencyProperty.Register("PulseWidth1", typeof(int),
typeof(WaitTicker_CustomControl),
new PropertyMetadata(20));
public int PulseWidth2
{
get { return (int)GetValue(PulseWidth2Property); }
set { SetValue(PulseWidth2Property, value); }
}
public static readonly DependencyProperty PulseWidth2Property =
DependencyProperty.Register("PulseWidth2", typeof(int),
typeof(WaitTicker_CustomControl),
new PropertyMetadata(20));
public int PulseWidth3
{
get { return (int)GetValue(PulseWidth3Property); }
set { SetValue(PulseWidth3Property, value); }
}
public static readonly DependencyProperty PulseWidth3Property =
DependencyProperty.Register("PulseWidth3", typeof(int),
typeof(WaitTicker_CustomControl),
new PropertyMetadata(0));
public Brush ControlBackground1
{
get { return (Brush)GetValue(ControlBackground1Property); }
set { SetValue(ControlBackground1Property, value); }
}
public static readonly DependencyProperty ControlBackground1Property =
DependencyProperty.Register("ControlBackground1", typeof(Brush),
typeof(WaitTicker_CustomControl),
new PropertyMetadata(Brushes.Blue));
public Brush ControlBackground2
{
get { return (Brush)GetValue(ControlBackground2Property); }
set { SetValue(ControlBackground2Property, value); }
}
public static readonly DependencyProperty ControlBackground2Property =
DependencyProperty.Register("ControlBackground2", typeof(Brush),
typeof(WaitTicker_CustomControl),
new PropertyMetadata(Brushes.Blue));
public Brush ControlBackground3
{
get { return (Brush)GetValue(ControlBackground3Property); }
set { SetValue(ControlBackground3Property, value); }
}
public static readonly DependencyProperty ControlBackground3Property =
DependencyProperty.Register("ControlBackground3", typeof(Brush),
typeof(WaitTicker_CustomControl),
new PropertyMetadata(Brushes.Blue));
private Beating _timer1;
private Beating _timer2;
private Beating _timer3;
public int PulseIncrementValue
{
get { return (int)GetValue(PulseIncrementValueProperty); }
set { SetValue(PulseIncrementValueProperty, value); }
}
public static readonly DependencyProperty PulseIncrementValueProperty =
DependencyProperty.Register("PulseIncrementValue", typeof(int),
typeof(WaitTicker_CustomControl),
new PropertyMetadata(1));
public int PulseMaxValue
{
get { return (int)GetValue(PulseMaxValueProperty); }
set { SetValue(PulseMaxValueProperty, value); }
}
public static readonly DependencyProperty PulseMaxValueProperty =
DependencyProperty.Register("PulseMaxValue", typeof(int),
typeof(WaitTicker_CustomControl),
new PropertyMetadata(50));
public int PulseMinValue
{
get { return (int)GetValue(PulseMinValueProperty); }
set { SetValue(PulseMinValueProperty, value); }
}
public static readonly DependencyProperty PulseMinValueProperty =
DependencyProperty.Register("PulseMinValue", typeof(int),
typeof(WaitTicker_CustomControl),
new PropertyMetadata(10));
public double PulseSpeedValue
{
get { return (double)GetValue(PulseSpeedValueProperty); }
set { SetValue(PulseSpeedValueProperty, value); }
}
public static readonly DependencyProperty PulseSpeedValueProperty =
DependencyProperty.Register("PulseSpeedValue", typeof(double),
typeof(WaitTicker_CustomControl),
new PropertyMetadata(0.01));
public int PulseCurrentValue1
{
get { return (int)GetValue(PulseCurrentValue1Property); }
set { SetValue(PulseCurrentValue1Property, value); }
}
public static readonly DependencyProperty PulseCurrentValue1Property =
DependencyProperty.Register("PulseCurrentValue1", typeof(int),
typeof(WaitTicker_CustomControl),
new PropertyMetadata(5));
public int PulseCurrentValue2
{
get { return (int)GetValue(PulseCurrentValue2Property); }
set { SetValue(PulseCurrentValue2Property, value); }
}
public static readonly DependencyProperty PulseCurrentValue2Property =
DependencyProperty.Register("PulseCurrentValue2", typeof(int),
typeof(WaitTicker_CustomControl),
new PropertyMetadata(20));
public int PulseCurrentValue3
{
get { return (int)GetValue(PulseCurrentValue3Property); }
set { SetValue(PulseCurrentValue3Property, value); }
}
public static readonly DependencyProperty PulseCurrentValue3Property =
DependencyProperty.Register("PulseCurrentValue3", typeof(int),
typeof(WaitTicker_CustomControl),
new PropertyMetadata(40));
private Beating CreateBeating(string beatName, DependencyProperty valueProperty, Border pulse)
{
Beating result = new Beating((int)GetValue(valueProperty), PulseIncrementValue, PulseMinValue, PulseMaxValue, beatName, PulseSpeedValue);
SetBinding(PulseIncrementValueProperty, new Binding(nameof(Beating.Increment)) { Source = result, Mode = BindingMode.OneWayToSource });
SetBinding(PulseMinValueProperty, new Binding(nameof(Beating.MinValue)) { Source = result, Mode = BindingMode.OneWayToSource });
SetBinding(PulseMaxValueProperty, new Binding(nameof(Beating.MaxValue)) { Source = result, Mode = BindingMode.OneWayToSource });
SetBinding(PulseSpeedValueProperty, new Binding(nameof(Beating.Speed)) { Source = result, Mode = BindingMode.OneWayToSource });
pulse.SetBinding(FrameworkElement.HeightProperty, new Binding(nameof(Beating.Value)) { Source = result, Mode = BindingMode.OneWay });
return result;
}
public WaitTicker_CustomControl()
{
InitializeComponent();
_timer1 = CreateBeating("Pulse1", PulseCurrentValue1Property, Pulse1);
_timer2 = CreateBeating("Pulse2", PulseCurrentValue2Property, Pulse2);
_timer3 = CreateBeating("Pulse3", PulseCurrentValue3Property, Pulse3);
}
}
internal sealed class Beating : INotifyPropertyChanged
{
private readonly Timer? _timer;
private int _value;
private bool _direction;
public int Value
{
get { return _value; }
set
{
if (value != _value)
{
_value = value;
OnPropertyChanged();
}
}
}
public int Increment { get; set; }
public int MinValue { get; set; }
public int MaxValue { get; set; }
public string BeatName { get; }
public double Speed
{
get { return _timer.Interval; }
set { _timer.Interval = value; }
}
public event PropertyChangedEventHandler? PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public Beating(int initialValue, int increment, int minValue, int maxValue, string beatName, double speed)
{
_value = initialValue;
Increment = increment;
MinValue = minValue;
MaxValue = maxValue;
BeatName = beatName;
_timer = new Timer
{
Interval = speed,
AutoReset = true,
Enabled = true,
};
_timer.Elapsed += OnTimedEvent;
}
public void StartTimer()
{
_timer.Start();
}
public void StopTimer()
{
_timer.Stop();
}
private void OnTimedEvent(Object source, ElapsedEventArgs e)
{
if (!_direction)
{
Value -= Increment;
if (Value < MinValue)
_direction = true;
}
else
{
Value += Increment;
if (Value > MaxValue)
_direction = false;
}
}
}
Custom Control Implementation
<Window x:Class="Testing.MainWindow"
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:Testing" xmlns:customcontrols="clr-namespace:Testing.CustomControls"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<customcontrols:WaitTicker_CustomControl
Height="100" Width="100"
VerticalAlignment="Top" HorizontalAlignment="Left"
Margin="10 10 0 0"
PulseMaxValue="100"
PulseMinValue="5"
PulseHeight1="100"
PulseHeight2="100"
PulseHeight3="100"
PulseSpeedValue="50"
ControlCornerRadius1="5"
ControlCornerRadius2="5"
ControlCornerRadius3="5"
ControlBackground1="Aquamarine"
ControlBackground2="Yellow"
ControlBackground3="Orange"/>
</Grid>
</Window>
modified 3-Sep-22 18:15pm.
|