如何以格式方式将Click事件添加到FrameworkElementFactory

时间:2017-11-20 21:23:34

标签: c# wpf

我试图以编程方式在代码后面应用数据模板。我设置的属性很好,但我仍然坚持如何添加Click事件。请问我该怎么做?

正在复制的Xaml代码

<DataTemplate>
                <telerik:RadToggleButton Content="+" 
                                         Width="20" 
                                         Height="20" 
                                         HorizontalContentAlignment="Center"                                                         
                                         VerticalContentAlignment="Center"       
                                         Click="RiskButton_OnClick"
                                         Visibility="{Binding AttachmentVisibility, Mode=TwoWay, Converter={StaticResource BooleanToVisibilityConverter}}"/>
</DataTemplate>

我目前拥有什么

var toggleButton = new FrameworkElementFactory(typeof(RadToggleButton));
toggleButton.SetValue(RadToggleButton.ContentProperty, "+");
toggleButton.SetValue(RadToggleButton.WidthProperty, 20.0);
toggleButton.SetValue(RadToggleButton.HeightProperty, 20.0);
toggleButton.SetValue(RadToggleButton.HorizontalContentAlignmentProperty, HorizontalAlignment.Center);
toggleButton.SetValue(RadToggleButton.VerticalAlignmentProperty, VerticalAlignment.Center);
//toggleButton.AddHandler(); <- what goes here?

DataTemplate dt = new DataTemplate
{
    VisualTree = toggleButton
};

dt.Seal();

1 个答案:

答案 0 :(得分:3)

toggleButton.AddHandler(RadToggleButton.ClickEvent, new RoutedEventHandler(SomeHandler));

SomeHandler是RoutedEventHandler:

public void SomeHandler(object sender, RoutedEventArgs e) 
{
}