何时在WPF中使用用户控件 [英] When to use User Controls in WPF

查看:250
本文介绍了何时在WPF中使用用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看法,过滤一个DataGrid,我想使用相同的集合的完全相同的过滤(除了列将是不同的)在不同的视图。



当然,我不想在这个新视图中复制XAML,所以当用户控件是正确的事情吗?



我唯一的问题是,新的DataGrid将被放置在新视图的下面,这样可能吗?而且正如我上面所说的那样,这些列在过滤中可能会有所不同,所以在用户控制中再次有可能出现这种情况?



感谢

解决方案

想要创建一个UserControl一种新的UI控件,或者导出一个现有的控件,并增强其功能。



你需要的是一个样式,你想要设置certian属性您对证书价值的控制:

 < Style TargetType =Button> 
< Setter Property =BackgroundValue =Red/>
< Setter Property =Template>
< Setter.Value>
< ControlTemplate TargetType =Button>
< Grid>
< Rectangle Fill ={TemplateBinding Background/>
< ContentPresenter />
< / Grid>
< / ControlTemplate>
< /Setter.Value>
< / Setter>
< / Style>


I have a view that Filters a DataGrid and I want to use the the exact same filtering with the same collection (apart from the columns will be different) in a different view.

Of course I dont want to duplicate the XAML in this new view, so is this when a user control would be the correct thing to do?

My only problem is that a new DataGrid will be put underneath in the new view so would this be possible? And as I said above, the columns also may be different in the filtering, so again, possible within a user control?

Thanks

解决方案

A UserControl should be used when wanting to create a new kind of control for your UI, or derive an existing control and enhance its capabilities.

What you need sounds like a Style, where you want to set certian properties of your control to certian values:

<Style TargetType="Button">
  <Setter Property="Background" Value="Red"/>
  <Setter Property="Template">
     <Setter.Value>
         <ControlTemplate TargetType="Button">
             <Grid>
                 <Rectangle Fill="{TemplateBinding Background"/>
                 <ContentPresenter/>
             </Grid>
         </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

这篇关于何时在WPF中使用用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆