Всем привет, есть UsercControl
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
134.
135.
136.
137.
public partial class TimeControl : UserControl
{
public TimeControl()
{
InitializeComponent();
foreach (ToggleButton btn in WPFhelper.GetLogicalChildCollection<ToggleButton>(RootStackPanel))
{
btn.Click +=ToggleButton_Click;
}
ChangeValueBtn.Content = "+";
ChangeValueBtn.Click += (s,e)=>
{
Button btn= (Button) s;
if ((string)btn.Content== "+")
{
btn.Content = "-";
}
else
{
btn.Content = "+";
}
};
}
private void ToggleButton_Click(object sender, RoutedEventArgs e)
{
ToggleButton btn = (ToggleButton)sender;
Person dx =(Person) btn.DataContext;
int p = 0;
p = (string)ChangeValueBtn.Content == "+" ? 1 : -1;
if (btn.Name.Contains("start"))
{
//начало работы
if (btn.Name.Contains("day"))
{
//увеличиваем день
dx.start_work= dx.start_work.AddDays(p);
}
else if (btn.Name.Contains("mon"))
{
//увеличиваем месяц
dx.start_work = dx.start_work.AddMonths(p);
}
else if (btn.Name.Contains("Hr"))
{
dx.start_work = dx.start_work.AddHours(p);
}
else if (btn.Name.Contains("Min"))
{
//увеличиваем минуты
dx.start_work = dx.start_work.AddMinutes(p);
}
}
else
{
//конец работы
if (btn.Name.Contains("day"))
{
//увеличиваем день
dx.finish_work = dx.finish_work.AddDays(p);
}
else if (btn.Name.Contains("mon"))
{
//увеличиваем месяц
dx.finish_work = dx.finish_work.AddMonths(p);
}
else if (btn.Name.Contains("Hr"))
{
dx.finish_work = dx.finish_work.AddHours(p);
}
else if (btn.Name.Contains("Min"))
{
dx.finish_work = dx.finish_work.AddMinutes(p);
//увеличиваем минуты
}
}
}
public DateTime StartWorkProperty
{
get { return (DateTime )GetValue(StartWorkPropertyProperty); }
set { SetValue(StartWorkPropertyProperty, value); }
}
// Using a DependencyProperty as the backing store for StartWorkProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty StartWorkPropertyProperty =
DependencyProperty.Register("StartWorkProperty", typeof(DateTime ), typeof(TimeControl), new PropertyMetadata(DateTime.Today, StartWorkPropertyCallBack));
private static void StartWorkPropertyCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
TimeControl p = d as TimeControl;
p.MinTxt_start.Text = string.Format("{0:D2}", Convert.ToDateTime(e.NewValue).Minute);
p.HrTxt_start.Text = string.Format("{0:D2}", Convert.ToDateTime(e.NewValue).Hour);
p.dayTxt_start.Text = string.Format("{0:D2}", Convert.ToDateTime(e.NewValue).Day);
p.monTxt_start.Text = string.Format("{0:D2}", Convert.ToDateTime(e.NewValue).Month);
}
public DateTime FinishWorkProperty
{
get { return (DateTime )GetValue(FinishWorkPropertyProperty); }
set { SetValue(FinishWorkPropertyProperty, value); }
}
// Using a DependencyProperty as the backing store for FinishWorkProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty FinishWorkPropertyProperty =
DependencyProperty.Register("FinishWorkProperty", typeof(DateTime ), typeof(TimeControl), new PropertyMetadata(DateTime.Today, FinishWorkPropertyCallBack));
private static void FinishWorkPropertyCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
TimeControl p = d as TimeControl;
p.MinTxt_finish.Text = string.Format("{0:D2}", Convert.ToDateTime(e.NewValue).Minute);
p.HrTxt_finish.Text = string.Format("{0:D2}", Convert.ToDateTime(e.NewValue).Hour);
p.dayTxt_finish.Text = string.Format("{0:D2}", Convert.ToDateTime(e.NewValue).Day);
p.monTxt_finish.Text = string.Format("{0:D2}", Convert.ToDateTime(e.NewValue).Month);
}
}
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
<UserControl x:Class="DeliveryForecast.Controls.Additional.TimeControl"
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:DeliveryForecast.Controls.Additional"
x:Name="usrCntrl"
mc:Ignorable="d" >
<UserControl.Resources>
<Style TargetType="{x:Type ToggleButton}" BasedOn="{StaticResource TimeControlToggleButton}"/>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource TimeControlButton}"/>
</UserControl.Resources>
<StackPanel Orientation="Horizontal" x:Name="RootStackPanel">
<!--дата-->
<ToggleButton x:Name="dayBtn_start" VerticalAlignment="Center" >
<TextBlock x:Name="dayTxt_start" TextWrapping="Wrap" Text="12"
HorizontalAlignment="Center"
VerticalAlignment="Center"
/>
</ToggleButton>
<TextBlock TextWrapping="Wrap" Text="."
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="0,3,0,0"
FontFamily="Segoe Condensed" />
<ToggleButton x:Name="monBtn_start" VerticalAlignment="Center">
<TextBlock x:Name="monTxt_start"
TextWrapping="Wrap" Text="00"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</ToggleButton>
<TextBlock TextWrapping="Wrap" Text="/"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="0,3,0,0"
FontFamily="Segoe Condensed" />
<!--время-->
<ToggleButton x:Name="HrBtn_start" VerticalAlignment="Center" >
<TextBlock x:Name="HrTxt_start"
TextWrapping="Wrap" Text="{Binding HrProperty}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
/>
</ToggleButton>
<TextBlock TextWrapping="Wrap" Text=":"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="0,3,0,0"
FontFamily="Segoe Condensed" />
<ToggleButton x:Name="MinBtn_start" VerticalAlignment="Center">
<TextBlock x:Name="MinTxt_start"
TextWrapping="Wrap"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</ToggleButton>
<Border
BorderThickness="0 0 1 0"
BorderBrush="Black"
Margin="5 0 5 0 "/>
<!--окончание работы-->
<ToggleButton x:Name="dayBtn_finish" VerticalAlignment="Center" >
<TextBlock x:Name="dayTxt_finish" TextWrapping="Wrap" Text="12"
HorizontalAlignment="Center"
VerticalAlignment="Center"
/>
</ToggleButton>
<TextBlock TextWrapping="Wrap" Text="."
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="0,3,0,0"
FontFamily="Segoe Condensed" />
<ToggleButton x:Name="monBtn_finish" VerticalAlignment="Center">
<TextBlock x:Name="monTxt_finish"
TextWrapping="Wrap" Text="00"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</ToggleButton>
<TextBlock TextWrapping="Wrap" Text="/"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="0,3,0,0"
FontFamily="Segoe Condensed" />
<!--время-->
<ToggleButton x:Name="HrBtn_finish" VerticalAlignment="Center" >
<TextBlock x:Name="HrTxt_finish"
TextWrapping="Wrap" Text="{Binding HrProperty}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
/>
</ToggleButton>
<TextBlock TextWrapping="Wrap" Text=":"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="0,3,0,0"
FontFamily="Segoe Condensed" />
<ToggleButton x:Name="MinBtn_finish" VerticalAlignment="Center">
<TextBlock x:Name="MinTxt_finish"
TextWrapping="Wrap"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</ToggleButton>
<!--кнопки для увеличения-->
<Button BorderBrush="Gray"
BorderThickness="1"
Margin="5 0 0 0"
x:Name="ChangeValueBtn"
MinWidth ="{Binding ActualHeight, RelativeSource={RelativeSource Self}}"
MinHeight ="{Binding ActualWidth, RelativeSource={RelativeSource Self}}"/>
</StackPanel>
</UserControl>
Нужно создать такой же UserCintrol, только добавить к времени еще год. Соответственно пытаюсь сначала унаследоваться от TimeControl
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
namespace DeliveryForecast.Controls.Additional
{
/// <summary>
/// Логика взаимодействия для TimeDateYearControl.xaml
/// </summary>
public partial class TimeDateYearControl : TimeControl
{
public TimeDateYearControl()
{
InitializeComponent();
}
}
}
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
<local:TimeDateYearControl
x:Class="DeliveryForecast.Controls.Additional.TimeDateYearControl"
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:DeliveryForecast.Controls.Additional"
mc:Ignorable="d" >
<Grid>
</Grid>
</local:TimeDateYearControl>
Получаю ошибку XAML-редактора
"Тип "TimeDateYearControl" не поддерживает непосредственное содержимое."
Непонятно почему, он же от UserCintrol унаследован.
Пытаюсь сделать по-другому- создать один базовый класс для 2-х UserControl-ов
1.
2.
3.
public partial class BaseDateTimeControlClass:UserControl
{
}
Когда меняю класс предок в TimeControl на
1.
public partial class TimeControl : BaseDateTimeControlClass
, то у TimeControl отваливаются методы от Dependency object (GetValue и SetValue)
Как будто иерархия наследования прерывается. Кто-либо сталкивался с такой проблемой?