我需要允许用户垂直调整用户控件的大小。所以搜索引导我了解了如何使用Thumb类和模板的一些示例。所以我设置了模板:
<UserControl.Resources>
<!-- ResizeDecorator Template -->
<ControlTemplate x:Key="ResizeDecoratorTemplate" TargetType="{x:Type Control}">
<Grid>
<t:ResizeThumb Height="3" Cursor="ScrollNS" VerticalAlignment="Bottom" Margin="0 0 0 -4" ItemResizeCompleted="ResizeThumb_ItemResizeCompleted" />
</Grid>
</ControlTemplate>
<!-- Designer Item Template-->
<ControlTemplate x:Key="DesignerItemTemplate" TargetType="ContentControl">
<Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}">
<Control Template="{StaticResource ResizeDecoratorTemplate}" />
<ContentPresenter Content="{TemplateBinding ContentControl.Content}" />
</Grid>
</ControlTemplate>
</UserControl.Resources>
然后我设置了ResizeThumb类:
Public Class ResizeThumb
Inherits Thumb
Public Event ItemResizeCompleted(sender As Object, e As TimeSlotPanelItemResizeEventArgs)
Public Property id As Integer
Public Sub New()
AddHandler DragDelta, AddressOf ResizeThumb_DragDelta
End Sub
Private Sub ResizeThumb_DragDelta(sender As Object, e As DragDeltaEventArgs)
e.Handled = True
End Sub
Private Sub ItemResized(sender As Object, e As DragCompletedEventArgs) Handles Me.DragCompleted
Dim rs As ResizeThumb = CType(sender, ResizeThumb)
Dim gd As Grid = CType(rs.Parent, Grid)
Dim ee As New TimeSlotPanelItemResizeEventArgs
ee.id = id
ee.ResizeValue = e.VerticalChange
RaiseEvent ItemResizeCompleted(Me, ee)
End Sub
End Class
我的UserControls是动态添加的,所以我在运行时添加模板:
Dim si As New SchedItem(id, StartTime, Duration, SO, Title, UnitOfWork, BackgroundColor, Status, LastNote, Miles, Hours)
Dim cc As ContentControl = New ContentControl
cc.Content = si
cc.Template = FindResource("DesignerItemTemplate")
每个SchedItem都有一个id。由于ResizeThumb类有一个DragComplete事件,我提出一个事件来传回VerticalChange,我想传递调整大小的SchedItem的id。我尝试将它绑定到TemplatedParent但是不允许。 ResizeThumb唯一的父级是Grid。我尝试将网格的Tag绑定到TemplatedParent,但标签中没有任何内容。
所以现在我要问;有没有办法从ResizeThumb类的ItemResized事件处理程序访问SchedItem的id属性?如果是这样,怎么样?如果没有,替代方案?
答案 0 :(得分:0)
圣牛!我想到了。我花了大部分时间,但它终于有效了。只需要弄清楚要使用的正确绑定。所以在我的代码中,我将ContentControl的Tag设置为id。
Dim cc As ContentControl = New ContentControl
cc.Tag = id '<-- right here
cc.Content = si
cc.Template = FindResource("DesignerItemTemplate")
然后在XAML中,弄清楚如何将该标记绑定到包含ResizeThumb类的Tag属性的Grid。
<!-- ResizeDecorator Template -->
<ControlTemplate x:Key="ResizeDecoratorTemplate" TargetType="{x:Type Control}">
<Grid Tag="{Binding RelativeSource={RelativeSource AncestorType=ContentControl}, Path=Tag}">
<t:ResizeThumb Height="3" Cursor="ScrollNS" VerticalAlignment="Bottom" Margin="0 0 0 -4" ItemResizeCompleted="ResizeThumb_ItemResizeCompleted" />
</Grid>
</ControlTemplate>
现在在ItemResized事件中,我可以访问网格,它是发件人的父级,并将标记分配给事件的ID。
Private Sub ItemResized(sender As Object, e As DragCompletedEventArgs) Handles Me.DragCompleted
Dim rs As ResizeThumb = CType(sender, ResizeThumb)
Dim gd As Grid = CType(rs.Parent, Grid)
Dim ee As New TimeSlotPanelItemResizeEventArgs
ee.id = gd.Tag
ee.ResizeValue = e.VerticalChange
RaiseEvent ItemResizeCompleted(Me, ee)
End Sub
现在我在活动中获得了身份证明。