我DataTemplate为ListBox外部资源字典中的项目定义了以下内容:
<DataTemplate x:Key="MyListBoxItemTemplate" DataType="{x:Type entities:Track}">
<StackPanel>
<TextBlock Text="Here's the slider:" />
<Slider Name="MySlider" Height="23" Minimum="0" />
</StackPanel>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
我需要为Slider的ValueChanged事件提供事件处理程序方法.我不知道我应该在哪里编写代码,因为为模板中的控件指定事件处理程序是不切实际的.
我一直在谷歌搜索解决方案,发现我应该在OnApplyTemplate()方法的覆盖中添加事件处理程序.我的猜测是它应该看起来像这样或类似:
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
// Is the following initialization even going to work!?!?
Slider MySlider = this.FindName("MySlider") as Slider;
SeekSlider.ValueChanged +=
new RoutedPropertyChangedEventHandler<double>(SeekSlider_ValueChanged);
}
Run Code Online (Sandbox Code Playgroud)
但是我应该在哪里写这个方法?OnApplyTemplate覆盖仅适用于ControlTemplates还是包含在我的场景中?我应该提供ControlTemplate而不是DataTemplate吗?我提供的方法正文是否正确?
请帮忙.谢谢.