treeview.resources:它们可以被提取出来吗?

时间:2013-08-30 00:16:49

标签: wpf treeview

我在我的应用中的几个地方定义了以下树视图:

<TreeView.Resources>
    <!-- Default Data template for common ParamterType, this displays ParamterType into text box, and do required validation checks on user inputs -->
    <DataTemplate DataType="{x:Type src:ParameterDefinition}">
    <StackPanel Orientation="Horizontal" VerticalAlignment="Stretch">
        <!-- TextBlock resource definition for displaying validation error messages right after text box -->
        <StackPanel.Resources>
            <DataTemplate DataType="{x:Type ValidationError}">
                <TextBlock FontStyle="Italic" Foreground="Red" HorizontalAlignment="Right" Margin="4" Text="{Binding Path=ErrorContent}"/>
            </DataTemplate>
        </StackPanel.Resources>
        <!-- TextBlock for Name of ParameterType -->
        <TextBlock Text="{Binding Path=Name}" Width="200"/>
        <!-- TextBox for Value of ParameterType -->
        <TextBox Name="parameterTxt" HorizontalAlignment="Left" Width="50" TextAlignment="Right">
            <TextBox.Resources>
                <src:LvDataResource x:Key="max" BindingTarget="{Binding Path=MaxValue}" />
                <src:LvDataResource x:Key="min" BindingTarget="{Binding Path=MinValue}" />
                <src:LvDataResource x:Key="default" BindingTarget="{Binding Path=Default}" />
            </TextBox.Resources>
            <TextBox.Text>
                <Binding Path="Value" NotifyOnValidationError="True" ValidatesOnDataErrors="True" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay">
                    <Binding.ValidationRules>
                        <src:LvDataValidation 
                            MinimumValue="{src:LvDataResourceBinding DataResource={StaticResource min}}" 
                            MaximumValue="{src:LvDataResourceBinding DataResource={StaticResource max}}" 
                            DefaultValue="{src:LvDataResourceBinding DataResource={StaticResource default}}" />
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>
        <!-- TextBlock to display validation errors right after text box -->
        <TextBlock Width="20"/>
        <ContentPresenter Content="{Binding ElementName=parameterTxt, Path=(Validation.Errors).CurrentItem}"/>
    </StackPanel>
</DataTemplate> </TreeView.Resources>

有没有办法将这个treeview.resources提取到模板或其他东西,所以我不必拥有这段代码的副本?

1 个答案:

答案 0 :(得分:1)

您可以添加DataTemplate App.xaml或任何其他ReasourceDictionary

例:(App.xaml中)

<Application x:Class="WpfApplication8.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml"
             xmlns:src="namespace for src">
    <Application.Resources>

      <DataTemplate DataType="{x:Type src:ParameterDefinition}">
        // all the stuff
      </DataTemplate>

    </Application.Resources>
</Application>
相关问题