博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WPF代码注意事项,开发常见问题,知识总结
阅读量:5022 次
发布时间:2019-06-12

本文共 5042 字,大约阅读时间需要 16 分钟。

代码注意事项:

1、代码实现的样式赋值

  XXX.Style = TryFindResource("StyleName") as Style;

 

2.WPF中FindName方法的使用

 (1)简单的使用 前台代码:

<Button x:Name=
"btnName" 
Click=
"btnName_Click"
>Test Method FindName</Button>

  后台代码:

 private void btnName_Click(object sender, RoutedEventArgs e)

{

  Button b = FindName("btnName") as Button;

  MessageBox.Show(b.Name);

}

(2)在模板中使用FindName方法

<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>

 后台代码:

private 
void 
btnName_Click(
object 
sender, RoutedEventArgs e)
    
{
        
Button b = rootBtn.Template.FindName(
"btnName"
,rootBtn)
as 
Button;
        
MessageBox.Show(b.Name);
    
}

3.使用多个绑定参数

<Button Height=
"23" 
Command=
"{Binding AddCommand}"   
Content=
"计算"
                
HorizontalAlignment=
"Left" 
Margin=
"20,0,0,49" 
Name=
"button1" 
VerticalAlignment=
"Bottom" 
Width=
"75"
>
           
<Button.CommandParameter>
               
<MultiBinding Converter=
"{StaticResource ParameterConverter}"
>
                   
<Binding Path=
"Text" 
ElementName=
"textBox1"
/>
                   
<Binding Path=
"Text" 
ElementName=
"textBox2"
/>
               
</MultiBinding>
           
</Button.CommandParameter>

4、将代码写在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#代码

    5.1<Window><Grid><Button ...></Grid></window>

  等效代码如下:

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、ItemContainerGenerator.ContainerFromIndex方法的使用

TreeViewItem item = (TreeViewItem)myTreeView.ItemContainerGenerator.ContainerFromIndex(1);

7、WPF获取子控件和父控件方法 

public 
class 
Globals
    
{
        
/// <summary>
        
/// 获取父控件方法。该方法将根据当前控件,遍历查找其父控件是否存在。参数1是表示当前子控件名,参数2是要查询父控件名;
        
/// </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>
        
/// 该方法将根据当前控件,遍历查找其子控件是否存在。参数1是表示当前父控件名,参数2是要查询子控件名;
        
/// </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>
        
/// 该方法将把所有子控件作为List集合返回到客户端。
        
/// 其中第一个参数是父控件参数,而第二个参数是特定子控件名称,
        
/// 如果需要遍历全部子控件,第二个参数留空即可。
        
/// </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.GridSplitter控件的使用:

<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.UniformGrid控件中每行没列都相等

<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>

如图:

 

转载于:https://www.cnblogs.com/sjqq/p/7766392.html

你可能感兴趣的文章
[bzoj] 2453 维护数列 || 单点修改分块
查看>>
IIS版本变迁
查看>>
使用Gzip压缩提升WEB服务器性能
查看>>
BZOJ3884: 上帝与集合的正确用法 拓展欧拉定理
查看>>
mybatis09--自连接一对多查询
查看>>
myeclipse10添加jQuery自动提示的方法
查看>>
【eclipse jar包】在编写java代码时,为方便编程,常常会引用别人已经实现的方法,通常会封装成jar包,我们在编写时,只需引入到Eclipse中即可。...
查看>>
视频监控 封装[PlayCtrl.dll]的API
查看>>
软件工程APP进度更新
查看>>
Python 使用正则替换 re.sub
查看>>
CTF中那些脑洞大开的编码和加密
查看>>
简化工作流程 10款必备的HTML5开发工具
查看>>
c++ 调用外部程序exe-ShellExecuteEx
查看>>
Java进击C#——语法之知识点的改进
查看>>
IdentityServer流程图与相关术语
查看>>
BirdNet: a 3D Object Detection Framework from LiDAR information
查看>>
icon fonts入门
查看>>
【Django】如何按天 小时等查询统计?
查看>>
HDU2191(多重背包)
查看>>
测试用例(一)
查看>>