2013-08-30 54 views
1

我有我的應用程序在幾個地方定義了以下樹視圖:treeview.resources:它們可以被提取嗎?

<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

您可以添加DataTemplateApp.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> 
相關問題