小编Tra*_*vis的帖子

WPF设计时与运行时风格的差异与触发器

我在设计时与运行时如何呈现XAML存在很大问题.在大多数情况下,事情是一致的,但是当我使用任何具有触发器的样式时,触发器不会在设计时检查.

下面是一个示例应用程序,用于显示事物的显示方式:

<Window x:Class="DesignDifferencesWithDesignAndRuntime.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="400" Width="400">
<Window.Resources>
    <Style x:Key="multiLineInTrigger" TargetType="{x:Type TextBox}">
        <Setter Property="HorizontalAlignment" Value="Left" />
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="Width" Value="150" />
        <Setter Property="Height" Value="22" />
        <Setter Property="BorderBrush" Value="Blue" />
        <Setter Property="BorderThickness" Value="2" />
        <Style.Triggers>
            <Trigger Property="AcceptsReturn" Value="True">
                <Setter Property="Width" Value="Auto" />
                <Setter Property="Height" Value="Auto" />
                <Setter Property="HorizontalAlignment" Value="Stretch" />
                <Setter Property="VerticalAlignment" Value="Stretch" />
            </Trigger>
        </Style.Triggers>
    </Style>
    <Style x:Key="singleLineInTrigger" TargetType="{x:Type TextBox}">
        <Setter Property="HorizontalAlignment" Value="Left" />
        <Setter Property="Width" Value="Auto" />
        <Setter Property="Height" Value="Auto" />
        <Setter Property="HorizontalAlignment" Value="Stretch" …
Run Code Online (Sandbox Code Playgroud)

wpf triggers styles design-time runtime

7
推荐指数
1
解决办法
2432
查看次数

EF4 LINQ使用Eager Loading(.Include())对父级和所有子集合进行排序

我在网格上进行分层数据绑定,我需要让数据库服务器对我的对象执行排序.我可以轻松地对父集合进行排序,但我似乎无法弄清楚如何对所有子集合进行排序.我有一个模型,其中嵌套了3个子集合,并且所有这些集合都需要进行排序.

这是我想要完成的示例模型:

    public class Year
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Make> Makes { get; set; }
}
public class Make
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Model> Models { get; set; }
}
public class Model
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Color> Colors { get; set; }
} …
Run Code Online (Sandbox Code Playgroud)

sorting linq-to-entities entity-framework eager-loading

4
推荐指数
1
解决办法
3561
查看次数

永久启用的滚动条可能吗?

我试图找到一种ScrollBars/ScrollViewers永久启用的方法时遇到问题.我正在开发一个数据输入应用程序,其中某些用户可能只具有"读取"权限.这需要禁用所有控件,以便它们无法进行任何更改.

禁用控件时,用户无法查看可滚动控件中的所有信息(Infragistics XamDataGrid,DevExpress GridControl,TextBoxes with Wrap ListBox等).我希望我能"智取"微软,并Trigger在设置IsEnabled为false时将其设置为true,但当然,它不起作用(出于各种原因,我敢肯定).

我一直在严格关注文本框,试图禁用内容,但保持ScrollViewer启用并没有运气.

我希望可能有一个解决方案,IE覆盖始终返回true 的IsEnabled依赖属性ScrollViewer,或者提供某种Style不允许ScrollViewer被禁用的东西.

我知道TextBox有一个IsReadOnly属性,但这对我解决此问题所需的许多其他控件没有用.

有没有什么方法可以用一种相当简单的方式来解决这个问题(简单的方法就是我可以制作一个适用于所有地方的样式,而无需更改200多个代码文件).

谢谢

编辑: 这是我在示例项目中使用的代码,试图解决此问题.

<Window x:Class="WPFScrollViewerStyles.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <Style BasedOn="{x:Null}" TargetType="{x:Type TextBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">
                    <Microsoft_Windows_Themes:ListBoxChrome x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderFocused="{TemplateBinding IsKeyboardFocusWithin}" SnapsToDevicePixels="true">
                        <ScrollViewer x:Name="PART_ContentHost" HorizontalScrollBarVisibility="Visible" 
                                      SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" IsEnabled="True" />
                    </Microsoft_Windows_Themes:ListBoxChrome> …
Run Code Online (Sandbox Code Playgroud)

wpf styles scrollbar scrollviewer

4
推荐指数
1
解决办法
4655
查看次数

WPF-使用样式为每个TreeViewItem根节点设置不同的ToggleButton图像

我是WPF中使用样式,资源和模板的新手。我需要做的是将TreeView中的ToggleButton +/-重写为一个图像,每个TreeViewItem根节点都有一个不同的图像。例如,我需要“ Car”节点的汽车图像和“ Plane”节点的飞机图像。我每个都有彩色和灰度图像(用于展开/折叠)。

我发现样式可以覆盖树视图并为切换按钮设置图像,但是我不确定以不同方式设置每个项目的最佳方法。

项目的样式代码很长,因此我敢肯定,有一种比复制/粘贴完整样式来更改source属性更好的方法。

有人可以指出正确的方向,以最好的方式做到这一点吗?

谢谢。

这是我一直在使用的风格,它是从另一篇文章中复制并为我的图像更改的。

    <Style x:Key="TreeViewItemFocusVisual">
    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate>
                <Rectangle/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="ExpandCollapseToggleStyle" TargetType="{x:Type ToggleButton}">
    <Setter Property="Focusable" Value="False"/>
    <Setter Property="Width" Value="16"/>
    <Setter Property="Height" Value="16"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ToggleButton}">
                <Border Width="16" Height="16" Background="Transparent">
                    <Border Width="16" Height="16" SnapsToDevicePixels="true" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" BorderBrush="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" BorderThickness="1">
                        <Image x:Name="ExpandImg" Width="16" Height="16" Source="/MyApp;component/Images/Icons/Grayscale/car.ico" />
                    </Border>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsChecked" Value="True">
                        <Setter Property="Source" TargetName="ExpandImg" Value="/MyApp;component/Images/Icons/Color/car.ico"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="TreeViewItemStyle1" TargetType="{x:Type TreeViewItem}"> …
Run Code Online (Sandbox Code Playgroud)

wpf resources styles treeviewitem togglebutton

2
推荐指数
1
解决办法
1万
查看次数