in wp / silverlight / wpf control template will often see. Often confused should be DataTemplate and ControlTemplate, this article will talk about two clothes DataTemplate and ControlTemplate relationship. This article will mainly dominated by wpf control so that we can finally open source.
one, ContentControl in DataTemplate
Before you begin, let's take a look ContentControl definition, both in wp or in wpf it has the following two properties:
public object Content {get; set;}
public DataTemplate ContentTemplate {get; set;}
Its characteristics are only accommodate a content, content type is object type, where the Button control is something we are more familiar with and belong to the ContentControl class, let's look at the direct use of TextBlock as its content, Button will work fine. As shown below:
This is normal, because the content is the object of the thing, then the following I will use another brush as its content. Look at the results:
display character string after conversion into the brush. If you are, then look back at the DataTemplate, you will find a summary of which is:
I let button belowGets or sets the content for display System.Windows.Controls.ContentControl data template. Content data template that is what content to show it.
content - brush in a circle on the display.
I use the button abovecontent control is achieved with a round button to show the content. Of course, by extension, I can use any content controls, first set its Content (which may be any complex content), and then use a DataTemplate to express Content data. Here we use a UserControl control to implement a student information: Defining a Student class, then initialize a stu, set it as UC's Content, and then set out the Content data.
from the above two examples can be concluded that the ContentControl Content DataTemplate is used to indicate the data, ie DataTemplate Content is the source of the binding, the specific form of the decision by the DataTemplate.
two, Control of ControlTemplate
in Control, there is a Template property, its summary and returns the result as follows:
/ / Summary:
/ / Gets or sets a control template.
/ / return the result:
/ / used to define System.Windows.Controls.Control the appearance of the template.
public ControlTemplate Template {get; set;}
and DataTemplate is not the same: define the appearance of the control template. We have also to Button as examples. The above returns the result to say that the appearance of the template, then I want a rounded Button, should belong to the scope of the appearance, and soon I thought of using the Border. Below to start it. I would like to get hold of the button, to write him on the content and add background color:
then add the Template property and found that the background color and content are gone.
if it is in accordance with this wording, the above results can be seen without abc the Content. To show abc if I could add a control inside the Border TextBlock, and then write on the TextBlock few letters found to show, but if it is not written abc, still can not show abc, description now displays the contents of the decided TextBlock, if there is a binding, then much fun, I can make TextBlock displays and abc's agreement. There TemplateBinding, when used to label the ControlTemplate use Target. Effect is as follows:
although the effect is achieved, but there is a very serious problem is the Button's Content is of type object, Text is a string type. If the button what time a bad mood, Content property became Image, it was not what I want to follow the "Button" bullied it? To free from gas, there is no use thinking about all types of content in containers it? The answer is yes. Use ContentPresenter. Now whether you are a picture or text, I do not have a bird you have ContentPresenter shining. The following light out of his way
ContentPresenter pretty standard, and he will give you automatic matching Target's Content and ContentTemplate. If Button.Content related attributes (such as FontSize, Foreground, FontFamily, etc.) changes, do not make any changes to the ContentPresenter will help us a good deal, but if it is, and Button itself related properties (such as background color, etc.), requires explicit adjustments.
three, DataTemplate and ControlTemplate Contact
In the example above, use ContentPresenter, you will find it also has a ContentTemplate, is not it will guess is DataTemplate type, and is in the Template tree "long with." It can guess the corresponding DataTemplate ContentTemplate is Template ControlTemplate tree corresponding to a sub-tree. To prove this fact, here I have to explain to Button.
now I were to use the button inside the text, images, brushes, as the button's content, and then add a container to display the Template subtree, the following is xaml code:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Grid.Row="0" Grid.Column="0"
Content="Click to Dump"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Click="OnButtonClick" />
<Button Grid.Row="0" Grid.Column="1"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Click="OnButtonClick">
<Image Source="Images/vs.png"
Stretch="None" />
</Button>
<Button Grid.Row="1" Grid.ColumnSpan="2"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Click="OnButtonClick">
<Button.Content>
<RadialGradientBrush>
<GradientStop Offset="0" Color="Blue" />
<GradientStop Offset="1" Color="AliceBlue" />
</RadialGradientBrush>
</Button.Content>
<Button.ContentTemplate>
<DataTemplate>
<Ellipse Width="100" Height="100" Fill="{Binding}" />
</DataTemplate>
</Button.ContentTemplate>
</Button>
<ScrollViewer Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2"
HorizontalScrollBarVisibility="Auto">
<StackPanel Name="stackPanel" />
</ScrollViewer>
</Grid>
I can find the visual tree (VisualTreeHelper class provides a way to combine recursive algorithm) to view the Template Button, which has the following sub-tree. The following code is the code behind:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
void OnButtonClick(object sender, RoutedEventArgs args)
{
Button btn = sender as Button;
stackPanel.Children.Clear();
DumpVisualTree(btn, 0);
}
void DumpVisualTree(DependencyObject parent, int indent)
{
TextBlock txtblk = new TextBlock();
txtblk.Text = String.Format("{0}{1}", new string(' ', 4 * indent),
parent.GetType().Name);
stackPanel.Children.Add(txtblk);
int numChildren = VisualTreeHelper.GetChildrenCount(parent);
for (int childIndex = 0; childIndex < numChildren; childIndex++)
{
DependencyObject child = VisualTreeHelper.GetChild(parent, childIndex);
DumpVisualTree(child, indent + 1);
}
}
}
click on each individual button, you can see how each button Template is constructed, there is a common characteristic visual tree contains a ContentPresenter, which not explain the DataTemplate is ContentPresenter replace lost, indicating that the DataTemplate generated The following is a ContentPresenter tree (wp and silverlight in ContentPresenter following tree may be somewhat different wpf above). ControlTemplate DataTemplate is also verified as part of the sub-tree.
IV Summary
This paper mainly introducesDataTemplate and ControlTemplate, then introduce ContentPresenter, through the visual tree helper class VisualTreeHelper Class View control contains template content, and then verify the DataTemplate and ControlTemplate relationship. If you think this article does not say where there is a place, please correct me! Thanks for reading!
codes download: http://files.cnblogs.com/lzhp/TemlateDemo.zip
OK in that case your user has access to the path/file in question. Are you running this application in a Linux environment? If so, I'm thinking it could be something to do with the path String.
回复删除