Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DependencyProperties #10

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions CV19/Components/GaugeIndicator.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<UserControl x:Class="CV19.Components.GaugeIndicator"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" mc:Ignorable="d"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:CV19.Components"
d:DesignHeight="100" d:DesignWidth="100">
<!--Background="{x:Null}"-->
<Grid>
<Path Stroke="Red" StrokeThickness="2" Data="M0,0 L5,7 L30,60 L20,0 H30 V40 Z" Visibility="Collapsed">
<!--<Path.Data>
<EllipseGeometry RadiusX="10" RadiusY="20"/>
</Path.Data>-->
</Path>
<!--<Polygon Stroke="Red" StrokeThickness="2" Fill="Blue"
Points="0,0 5,7 4,8 10,2"/>-->
<!--<Polyline StrokeThickness="2" Stroke="Red" Points="5,7 10,12 20,27"/>-->
<!--<Line Stroke="Red" StrokeThickness="2"
X1="5" Y1="7" X2="50" Y2="50"/>-->
<!--<Line StrokeThickness="2" Stroke="Red"
X2="{Binding ActualWidth, RelativeSource={RelativeSource Self}}"
Y2="{Binding ActualHeight, RelativeSource={RelativeSource Self}}"/>-->
<!--<Border BorderThickness="0,4,0,0" BorderBrush="Red" CornerRadius="5,5,0,0"/>-->
<Viewbox>
<Grid Width="100" Height="100">
<Line StrokeThickness="2" Stroke="Red"
X1="50" X2="50"
Y1="80" Y2="20"
RenderTransformOrigin="0.5,0.8">
<Line.RenderTransform>
<TransformGroup>
<RotateTransform Angle="-50"/>
<RotateTransform Angle="{Binding Value, RelativeSource={RelativeSource AncestorType=local:GaugeIndicator}}"
x:Name="ArrowRotator"/>
</TransformGroup>
</Line.RenderTransform>
</Line>
</Grid>
</Viewbox>
</Grid>
</UserControl>
66 changes: 66 additions & 0 deletions CV19/Components/GaugeIndicator.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;
using System.ComponentModel;
using System.Windows;

namespace CV19.Components
{
public partial class GaugeIndicator
{
#region ValueProperty

public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register(
nameof(Value),
typeof(double),
typeof(GaugeIndicator),
new PropertyMetadata(
default(double),
OnValuePropertyChanged,
OnCoerceValue),
OnValidateValue);

private static bool OnValidateValue(object O)
{
return true;
}

private static object OnCoerceValue(DependencyObject D, object Basevalue)
{
var value = (double) Basevalue;
return Math.Max(0, Math.Min(100, value));
}

private static void OnValuePropertyChanged(DependencyObject D, DependencyPropertyChangedEventArgs E)
{
//var gauge_indicator = (GaugeIndicator) D;
//gauge_indicator.ArrowRotator.Angle = (double) E.NewValue;
}

public double Value
{
get => (double)GetValue(ValueProperty);
set => SetValue(ValueProperty, value);
}

#endregion

#region Angle : double - Какой-то угол

/// <summary>Какой-то угол</summary>
public static readonly DependencyProperty AngleProperty =
DependencyProperty.Register(
nameof(Angle),
typeof(double),
typeof(GaugeIndicator),
new PropertyMetadata(default(double)));

/// <summary>Какой-то угол</summary>
[Category("Моя категория!")]
[Description("Какой-то угол")]
public double Angle { get => (double) GetValue(AngleProperty); set => SetValue(AngleProperty, value); }

#endregion

public GaugeIndicator() => InitializeComponent();
}
}
22 changes: 22 additions & 0 deletions CV19/Infrastructure/Converters/DebugConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

namespace CV19.Infrastructure.Converters
{
internal class DebugConverter : Converter
{
public override object Convert(object v, Type t, object p, CultureInfo c)
{
System.Diagnostics.Debugger.Break();
return v;
}

public override object ConvertBack(object v, Type t, object p, CultureInfo c)
{
System.Diagnostics.Debugger.Break();
return v;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.ComponentModel;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace CV19.Infrastructure.Converters
{
internal class ParametricMultiplicityValueConverter : Freezable, IValueConverter
{
#region Value : double - Прибавляемое значение

/// <summary>Прибавляемое значение</summary>
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register(
nameof(Value),
typeof(double),
typeof(ParametricMultiplicityValueConverter),
new PropertyMetadata(1.0/*, (d,e) => { }*/));

/// <summary>Прибавляемое значение</summary>
//[Category("")]
[Description("Прибавляемое значение")]
public double Value { get => (double) GetValue(ValueProperty); set => SetValue(ValueProperty, value); }

#endregion

public object Convert(object v, Type t, object p, CultureInfo c)
{
var value = System.Convert.ToDouble(v, c);

return value * Value;
}

public object ConvertBack(object v, Type t, object p, CultureInfo c)
{
var value = System.Convert.ToDouble(v, c);

return value / Value;
}

protected override Freezable CreateInstanceCore() => new ParametricMultiplicityValueConverter { Value = Value };
}
}
4 changes: 2 additions & 2 deletions CV19/ViewModels/Base/ViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public override object ProvideValue(IServiceProvider sp)
private WeakReference _TargetRef;
private WeakReference _RootRef;

public object TargetObject => _TargetRef.Target;
public object TargetObject => _TargetRef?.Target;

public object RootObject => _RootRef.Target;
public object RootObject => _RootRef?.Target;

protected virtual void OnInitialized(object Target, object Property, object Root)
{
Expand Down
20 changes: 20 additions & 0 deletions CV19/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,26 @@ public string Status

#endregion

#region FuelCount : double - Количество непонятно чего

/// <summary>Количество непонятно чего</summary>
private double _FuelCount;

/// <summary>Количество непонятно чего</summary>
public double FuelCount { get => _FuelCount; set => Set(ref _FuelCount, value); }

#endregion

#region Coefficient : double - Коэффициент

/// <summary>Коэффициент</summary>
private double _Coefficient = 1;

/// <summary>Коэффициент</summary>
public double Coefficient { get => _Coefficient; set => Set(ref _Coefficient, value); }

#endregion

public IEnumerable<Student> TestStudents =>
Enumerable.Range(1, App.IsDesignMode ? 10 : 100_000)
.Select(i => new Student
Expand Down
21 changes: 20 additions & 1 deletion CV19/Views/Windows/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
xmlns:sys="clr-namespace:System;assembly=System.Runtime"
xmlns:cm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
xmlns:system="clr-namespace:System;assembly=System.Runtime.Extensions"
xmlns:components="clr-namespace:CV19.Components"
xmlns:converters="clr-namespace:CV19.Infrastructure.Converters"
Title="{Binding Title}"
DataContext="{Binding MainWindowModel, Source={StaticResource Locator}}"
Width="800" Height="450">
Expand Down Expand Up @@ -55,7 +57,24 @@
</TabItem>

<TabItem Header="Отладка">
<!--<TextBlock Text="{x:Static system:Environment.CurrentDirectory}"/>-->
<Grid>
<Grid.Resources>
<converters:ParametricMultiplicityValueConverter x:Key="Mult" Value="{Binding Coefficient}"/>
</Grid.Resources>
<components:GaugeIndicator VerticalAlignment="Top" HorizontalAlignment="Left"
Width="80" Height="80">
<components:GaugeIndicator.Value>
<Binding Path="FuelCount" Converter="{StaticResource Mult}"/>
</components:GaugeIndicator.Value>
</components:GaugeIndicator>
<Slider VerticalAlignment="Top" HorizontalAlignment="Stretch" Margin="90,0,0,0"
Minimum="0" Maximum="100" Value="{Binding FuelCount}"
ToolTip="{Binding FuelCount}"/>
<Slider VerticalAlignment="Top" HorizontalAlignment="Stretch" Margin="90,40,0,0"
Minimum="-10" Maximum="10" SmallChange="0.1"
Value="{Binding Coefficient}"
ToolTip="{Binding Coefficient}"/>
</Grid>
</TabItem>
</TabControl>
</DockPanel>
Expand Down