Code considerations:
1. Code-implemented style assignment
Xxx. Style = tryfindresource ("stylename") as style;
2. Use of the findname method in WPF
(1) Simple foreground code:
<Button x:Name="btnName" Click="btnName_Click">Test Method FindName</Button>
Background code:
Private void btnname_click (Object sender, routedeventargs E)
{
Button B = findname ("btnname") as button;
MessageBox. Show (B. Name );
}
(2) Use the findname method in the template
<Grid> <Grid x:Name="childGrid"> <Button x:Name="rootBtn"> <Button.Template> <ControlTemplate> <Button x:Name="btnName" Click="btnName_Click">Test Method FindName</Button> </ControlTemplate> </Button.Template> </Button> </Grid> </Grid>
Background code:
private void btnName_Click(object sender, RoutedEventArgs e) { Button b = rootBtn.Template.FindName("btnName",rootBtn) as Button; MessageBox.Show(b.Name); }
3. Use multiple binding parameters
<Button Height = "23" command = "{binding addcommand}" content = "computing" horizontalalignment = "Left" margin = "20, 0, 0, 49 "name =" button1 "verticalignment =" bottom "width =" 75 "> <button. commandparameter> <multibinding converter = "{staticresource parameterconverter}"> <binding Path = "text" elementname = "textbox1"/> <binding Path = "text" elementname = "textbox2 "/> </multibinding> </button. commandparameter>
4. Write the code in XAML
<Grid> <x:Code> <![CDATA[ private void button1_Click(object sender, RoutedEventArgs e) { button1.Background = Brushes.Blue; button1.Content = "The code is in XAML"; MessageBox.Show("hi"); } ]]></x:Code> <Button Height="23" Margin="46,56,32,0" Name="button1" VerticalAlignment="Top" Click="button1_Click">Click me!</Button> </Grid>
5. C # code corresponding to each attribute
5.1 <WINDOW> <grid> <button...> </GRID> </WINDOW>
The equivalent code is as follows:
Grid g = new Grid(); Button b = new Button(); b.Width = 100; b.Height = 100; b.Content = "Test Button"; b.Foreground = Brushes.LightBlue; g.Children.Add(b); myWindow.AddChild(g);
6. Use the itemcontainergenerator. containerfromindex Method
TreeViewItem item = (TreeViewItem)myTreeView.ItemContainerGenerator.ContainerFromIndex(1);
7. How to obtain child and parent controls using WPF
Public class globals {// <summary> // obtain the parent control method. This method traverses the current control to check whether its parent control exists. Parameter 1 indicates the name of the current Child control, and parameter 2 indicates the name of the parent control to be queried; // </Summary> Public t getparentobject <t> (dependencyobject OBJ, string name) where T: frameworkelement {dependencyobject parent = visualtreehelper. getparent (OBJ); While (parent! = NULL) {If (parent is T & (t) parent ). name = Name | string. isnullorempty (name) {return (t) parent;} parent = visualtreehelper. getparent (parent) ;}return null ;}/// <summary> /// this method traverses the current control to check whether its subcontrol exists. Parameter 1 indicates the name of the current parent control, and parameter 2 indicates the name of the child control to be queried; // </Summary> Public t getchildobject <t> (dependencyobject OBJ, string name) where T: frameworkelement {dependencyobject child = NULL; t grandchild = NULL; For (INT I = 0; I <= visualtreehelper. getchildrencount (OBJ)-1; I ++) {Child = visualtreehelper. getchild (OBJ, I); If (child is T & (t) child ). name = Name | string. isnullorempty (name) {return (t) child;} else {Grandchild = getchildobject <t> (child, name); If (grandchild! = NULL) return grandchild;} return NULL;} // <summary> // This method returns all child controls to the client as a list set. /// The first parameter is the parent control parameter, and the second parameter is the Special Control name. // If You Need to traverse all child controls, leave the second parameter blank. /// </Summary> public list <t> getchildobjects <t> (dependencyobject OBJ, string name) where T: frameworkelement {dependencyobject child = NULL; list <t> childlist = new list <t> (); For (INT I = 0; I <= visualtreehelper. getchildrencount (OBJ)-1; I ++) {Child = visualtreehelper. getchild (OBJ, I); If (child is T & (t) child ). name = Name | string. isnullorempty (name) {childlist. add (t) Child);} childlist. addrange (getchildobjects <t> (child, "") ;}return childlist ;}}
XAML:
Globals VTHelper = new Globals(); StackPanel sp = VTHelper.GetChildObject<StackPanel>(this.LayoutRoot, "spDemoPanel"); Grid layoutGrid = VTHelper.GetParentObject<Grid>(this.spDemoPanel, "LayoutRoot"); List<TextBlock> textblock = VTHelper.GetChildObjects<TextBlock>(this.LayoutRoot, "");
8. Use of the gridsplitter control:
<Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="90*" /> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="180*" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="190*" /> <RowDefinition Height="Auto" /> <RowDefinition Height="70*" /> </Grid.RowDefinitions> <Button Content="ButtonA" Margin="3" Grid.Row="0" Grid.Column="0" Grid.RowSpan="3" /> <Button Content="ButtonB" Margin="3" Grid.Row="0" Grid.Column="2" /> <Button Content="ButtonC" Margin="3" Grid.Row="2" Grid.Column="2" /> <GridSplitter Width="3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="0" Grid.Column="1" Grid.RowSpan="3"></GridSplitter> <GridSplitter Height="3" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Grid.Row="1" Grid.Column="2"></GridSplitter> </Grid>
:
9. In the uniformgrid control, no columns in each row are equal.
<UniformGrid> <Button Content="ButtonA" /> <Button Content="ButtonB" /> <Button Content="ButtonC" /> <Button Content="ButtonD" /> <Button Content="ButtonE" /> <Button Content="ButtonF" /> <Button Content="ButtonG" /> <Button Content="ButtonH" /> </UniformGrid>