首页编程relativesource c#wpf 中有没有checklistbox

relativesource c#wpf 中有没有checklistbox

编程之家2023-11-03106次浏览

大家好,如果您还对relativesource不太了解,没有关系,今天就由本站为大家分享relativesource的知识,包括c#wpf 中有没有checklistbox的问题都会给大家分析到,还望可以解决大家的问题,下面我们就开始吧!

relativesource c#wpf 中有没有checklistbox

c#wpf 中有没有checklistbox

实际项目中常常要实现有CheckBox列表框。但是WPF没有自带这样的一个控件,下面就用Style来实现这样的功能。而对于CheckBox列表框,又常常会有一个Select All的CheckBox来表示当前列表框的选择状态。这个功能也会被包含在下面的示例之中。效果如下图所示。

对于单纯的,没有后台数据绑定的情况下,这个功能可以用ItemContainerStyle来实现。代码如下:

复制代码

CheckListBoxItemContainerStyle

<Style x:Key="CheckListBoxItemContainerStyle"

TargetType="{x:Type ListBoxItem}">

relativesource c#wpf 中有没有checklistbox

<!--Set it un-focusable, becaues the CheckBox in it should be focusable and only it.-->

<Setter Property="Focusable" Value="False"/>

<Setter Property="Template">

<Setter.Value>

<ControlTemplate TargetType="{x:Type ListBoxItem}">

<CheckBox Content="{TemplateBinding Content}"

relativesource c#wpf 中有没有checklistbox

ContentTemplate="{TemplateBinding ContentTemplate}"

ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}"

IsChecked="{Binding IsSelected, RelativeSource={RelativeSource TemplatedParent}}"/>

</ControlTemplate>

</Setter.Value>

</Setter>

</Style>

复制代码

其中要对Content和ContentTemplate等属性进行绑定,以方便对其进行扩展,保证其通用性。这个Style一般会放在Application级别的Resource中。

对于有后台数据绑定的情况,一般会有双个属性要绑定,一个是CheckBox里的Content,一个是CheckBox的IsChecked。绑定的路径,只有在用一个Style的ListBox那里才知道,所以并不能写在这个Style里,否则会破坏这个Style的通用性。比较合理的方式是基于这个现有的Style进行修改。

对于下面的数据类。

DataItem Class

我们需要下面这个有针对性的Style来应用数据绑定。

复制代码

DataItemCheckListBoxStyle

<Style x:Key="DataItemCheckListBoxStyle"

TargetType="{x:Type ListBox}"

BasedOn="{StaticResource{x:Type ListBox}}">

<Setter Property="ItemTemplate">

<Setter.Value>

<DataTemplate>

<TextBlock Text="{Binding Name}"/>

</DataTemplate>

</Setter.Value>

</Setter>

<Setter Property="ItemContainerStyle">

<Setter.Value>

<Style TargetType="{x:Type ListBoxItem}"

BasedOn="{StaticResource CheckListBoxItemContainerStyle}">

<Setter Property="IsSelected"

Value="{Binding IsEnabled}"/>

<Setter Property="Margin" Value="2,2,0,0"/>

</Style>

</Setter.Value>

</Setter>

<Setter Property="SelectionMode" Value="Multiple"/>

</Style>

复制代码

在上面的Style中,使用了ItemTemplate来指定CheckBox里的Content绑定到的属性,并把ListBoxItem的IsSelected绑定数据的相应属性上。由于这个Style是针对特定数据写的,所以应当放置在使用这个Style的ListBox所在的Window的Resource中。

当然,也可以为ListBox添加两个绑定类型的Attached Property来实现一个通用的Style。不过这个Property一样要在使用的地方设置,其实没有太大区别。有兴趣的读者可以自己试一下。

对于Select All这个CheckBox而言,用Attached Property倒是很方便。给CheckBox添加一个SyncTarget属性指向要同步的ListBox,就可以在Window.xaml.cs之外的地方同步CheckBox和ListBox了。代码如下:

ToggleButtonProperty

使用方式也很简单。如下代码所示。

复制代码

用法

<DockPanel Margin="12">

<CheckBox Content="Select All"

Margin="0,0,0,5"

DockPanel.Dock="Top"

ext:ToggleButtonProperty.SyncTarget="{Binding ElementName=checkListBox}"/>

<ListBox x:Name="checkListBox"

Style="{StaticResource DataItemCheckListBoxStyle}"

ItemsSource="{Binding Path=Items, ElementName=mainWindow}"/>

</DockPanel>

怎样自定义DevExpress Grid的打印外观

认情况下,

由于表格是一个“所见即所得”的控件,DXGrid以一种简单的表格格式被导出和打印。但是你仍然可以调节表格打印的外观。这篇文章讲述了怎样使用表格

的打印模板和选项来创建你自己特定的打印外观。但是,如果你想得到全部的自定义数据打印外观,欢迎你考虑使用我们的 XtraReports产品,它会提供给你一种创建自定义报告的强大而灵活的方式。

默认情况下,表格是作为一个简单的表来打印。它的宽度被调节成适应一个报告页面,你能够通过设置打印自动宽度为False来切换自动宽度。

你可以通过分别设置打印列表头和打印总摘要属性为false来从打印视图上移除头部和底部

每一个表格打印部分的外观可以被重定义。因此,你能使用PrintHeaderTemplate属性来设置自定义表头。

[XML]

<dxg:GridControl.Resources>

<DataTemplate x:Key="CustomPrintHeaderTemplate">

<Grid>

<Rectangle RadiusX="5" RadiusY="5" Fill="Red" dxp:ExportSettings.TargetType="Image"/>

<dxe:TextEdit Text="Custom header template"

IsPrintingMode="True"

Width="{Binding Path=Content.(dxg:GridPrintingHelper.PrintRowInfo).TotalHeaderWidth}"

FontSize="20"/>

</Grid>

</DataTemplate> you

</dxg:GridControl.Resources>

<dxg:GridControl.View>

<dxg:TableView PrintHeaderTemplate="{StaticResource CustomPrintHeaderTemplate}"/>

</dxg:GridControl.View>

如果你想获取表头模板的 column headers文本,你可以使用以下的绑定声明:

[XML]

<dxe:TextEdit Text="{Binding Path=Content.View.DataControl.Columns[ColumnName].HeaderCaption}"/>

注意:被放置在表头模板中的TextEdit拥有一个设置为true的

IsPrintMode属性,Rectangle控件有一个ExportSettings.TargetType。打印模板中所使用的所有控件必须被调整

以便打印。因此,任何从BaseEdit继承的控件都必须将IsPrintingMode属性设置为true。

其他的每个控件必须通过ExportSettings

属性来调节打印。因此,它必须要提供ExportSettings.TargetType属性值。这个属性被定义在

xmlns:dxp="http://schemas.devexpress.com/winfx/2008/xaml/printing"

命名空间中,并指定如何利用打印系统来利用该控件。

例如,如果你想打印TextBlock,

必须将ExportSettings.TargetType指定为文本,这样TextBlock才会被打印。如果你想设置一些控件的背景和前景,你应该分

别设置ExportSettings.Background和ExportSettings.Foreground。

[XML]

<TextBlock Text="Textblock text" Margin="10"Foreground="Red"dxp:ExportSettings.TargetType="Text"dxp:ExportSettings.Background="White"dxp:ExportSettings.Foreground="Red"/>

形状和复合控件可以作为图形导出:

[XML]

<Ellipse Fill="Yellow" Width="15" Height="15"dxp:ExportSettings.TargetType="Image"dxp:ExportSettings.Background="Transparent"/>

如何你的打印模板内有一些面板,你应该在面板中设置ExportSettings.TargetType属性,以便打印的子系统处理子元素。

[XML]

<StackPanel Orientation="Horizontal"dxp:ExportSettings.TargetType="Panel"><TextBlock Text="Property:" Margin="2" dxp:ExportSettings.TargetType="Text"/><TextBlock Text="Value" Margin="2" dxp:ExportSettings.TargetType="Text"/></StackPanel>

如果一个控件没有指定导出设置类型,控件将会被打印子系统忽视,它将会在打印文档中丢失。这也适用于在IsPrintingMode属性没有设置的编辑器中。

表格底部模板可以以它在头部模板中定义的方式来同样被定义。

[XML]

<dxg:GridControl.Resources>

<DataTemplate x:Key="CustomPrintFooterTemplate">

<dxe:TextEdit IsPrintingMode="True"

Width="{Binding Path=Content.(dxg:GridPrintingHelper.PrintRowInfo).TotalHeaderWidth}"

Text="{Binding Path=Path=Content.View.DataControl.Columns[ColumnName].TotalSummaryText, Mode=OneWay}"

/>

</DataTemplate>

</dxg:GridControl.Resources>

PrintRowTemplate属性定义了一个用于显示每一行表格的模板。在这里你可以放置你的行单元值去显示一个行内容。模板的

DataContext.Content属性中有一个RowData类对象。为了简化针对在这个模板上行单元值的绑定表达式

,你可以通过以下的代码来定义模板布局:

[XML]

<DataTemplate x:Key="CustomPrintRowTemplate">

<ContentControl Foreground="{Binding Foreground, RelativeSource={RelativeSource TemplatedParent}}">

<Grid DataContext="{Binding DataContext.Content.Row, RelativeSource={RelativeSource TemplatedParent}}"

Width="{Binding Path=DataContext.Content.(dxg:GridPrintingHelper.PrintRowInfo).TotalHeaderWidth, ElementName=contentControl}"

>

<Rectangle RadiusX="5" RadiusY="5" Fill="Red" dxp:ExportSettings.TargetType="Image"/>

<dxe:ImageEdit Source="{Binding Path=Photo}" IsPrintingMode="True"/>

<dxe:TextEdit EditValue="{Binding FirstName}" IsPrintingMode="True"/>

</Grid>

</ContentControl>

</DataTemplate>

非常感谢您的阅读!我们希望本文对于解决您关于relativesource的问题提供了一些有价值的信息。如果您还有其他疑问,我们将很乐意为您提供进一步的帮助。

宁波网站建设哪家好?宁波网站建设公司哪家好梧州网站设计 旅游景点网站 旅游景点网站毕业设计