WPF / C中的条件事件处理程序#

时间:2012-10-24 17:34:40

标签: c# wpf xaml

我有一个场景,我想根据数据模型中的几个条件在DockPanel / TextBlock上添加拖放数据的鼠标和键盘事件。如果该条件不存在,那么我不希望我的代码监听这些事件。

<!-- Data Template with all event handlers -->
<DataTemplate DataType="{x:Type ViewModels:EventsViewModel}">   
    <DockPanel Name="dpnMain">
                <Button Name="btnSourceLink" DockPanel.Dock="Right" HorizontalAlignment="Right" Padding="0"
                        ClickMode="Press"
                        Click="Button_OnMouseButtonUp"
                        PreviewMouseDown="Button_DragDataMouseDown"
                        Content="{Binding Value, Converter={StaticResource RegionalDataFormatConverter}}"
                        ToolTip="{Binding Tooltip}"
                        ToolTipService.IsEnabled="{Binding IsTooltipAvailable}" />
                <StackPanel Name="spnWithDragNDrop" DockPanel.Dock="Left">
                    <TextBlock HorizontalAlignment="Left" TextWrapping="NoWrap" ClipToBounds="True" Padding="0"
                               Text="{Binding Label}" MouseEnter="OnMouseEnter" MouseLeave="OnMouseLeave" 
                               MouseLeftButtonUp="OnMouseLeftButtonUp"
                               PreviewMouseDown="DragDataMouseDown"
                               ToolTip="{Binding Label}"
                               Width="{Binding ElementName=spn, Path=ActualWidth, Mode=OneWay,
                                Converter={StaticResource WidthFromParent}, ConverterParameter=10}"/>
                </StackPanel>
    </DockPanel>
<DataTemplate>


<!-- Data Template without event handlers -->
<DataTemplate DataType="{x:Type ViewModels:PlainViewModel}">    
    <DockPanel Name="dpnMain">
                <Button Name="btnSourceLink" DockPanel.Dock="Right" HorizontalAlignment="Right" Padding="0"
                        ClickMode="Press"
                        Click="Button_OnMouseButtonUp"
                        PreviewMouseDown="Button_DragDataMouseDown"
                        Content="{Binding Value, Converter={StaticResource RegionalDataFormatConverter}}"
                        ToolTip="{Binding Tooltip}"
                        ToolTipService.IsEnabled="{Binding IsTooltipAvailable}" />
                <StackPanel Name="spnWithoutDragNDrop" DockPanel.Dock="Left">
                    <TextBlock HorizontalAlignment="Left" TextWrapping="NoWrap" ClipToBounds="True" Padding="0"
                               Text="{Binding Label}"
                               ToolTip="{Binding Label}"
                               Width="{Binding ElementName=spn, Path=ActualWidth, Mode=OneWay,
                                Converter={StaticResource WidthFromParent}, ConverterParameter=10}"/>
                </StackPanel>
    </DockPanel>
<DataTemplate>

<!-- Content Control will select the appropriate data template based on the view model it is bound to -->
<ContentControl Focusable="False" Padding="0" Content="{Binding}"/>

我不想写两个不同的数据模板,并希望根据它的viewModel类型进行选择。我需要一些可以绑定到这些事件的ValueConverters。

无论如何我可以写一些转换器来避免编写另一个DataTemplates和View Model,转换器会调查幕后的那些条件吗?

1 个答案:

答案 0 :(得分:0)

使用以下代码创建一个类:

public class MyTemplateSelector : DataTemplateSelector
{
    private static DataTemplate _eventsViewModel;
    private static DataTemplate EventsViewModel
    {
        get
        {
            if (_eventsViewModel== null)
                _eventsViewModel= Application.Current.FindResource("EventsViewModel") as DataTemplate;
            return _eventsViewModel;
        }
    }

    private static DataTemplate _plainViewModel;
    private static DataTemplate PlainViewModel
    {
        get
        {
            if (_plainViewModel== null)
                _plainViewModel= Application.Current.FindResource("PlainViewModel") as DataTemplate;
            return _plainViewModel;
        }
    }

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        if(item is EventsViewModel) return EventsViewModel;
        else if(item is PlainViewModel) return PlainViewModel;

    }
}
资源标签中的xaml中的

<core:MyTemplateSelector x:Key="myTemplateSelector"/>
xaml中的

<ContentPresenter ContentSource="{Binding}" ContentTemplateSelector="{StaticResource myTemplateSelector}"/>