小编Vah*_*hid的帖子

在WPF中重构XAML

我的XAML很长很难维护.我想知道是否有办法做重构之类的事情?

这是一个简单的例子:

<Window x:Class="RefactorXAML.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel>
            <Button>New</Button>
            <Button>Open</Button>
        </StackPanel>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

我怎样才能重构该Stackpanel部分并写出这样的内容?

<Window x:Class="RefactorXAML.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        // Refactored Markup
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

c# wpf xaml

6
推荐指数
1
解决办法
1570
查看次数

使用LINQ更改List中对象的属性

我有Beam对象列表.IsJoistWidth使用LINQ属性大于40 时,如何更改梁的属性?

class Beam
{
    public double Width { get; set; }
    public bool IsJoist { get; set; }
}

var bm1 = new Beam { Width = 40 };
var bm2 = new Beam { Width = 50 };
var bm3 = new Beam { Width = 30 };
var bm4 = new Beam { Width = 60 };

var Beams = new List<Beam> { bm1, bm2, bm3, bm4 };
Run Code Online (Sandbox Code Playgroud)

这是我所做的,但我只是得到一个列表.我希望新列表与原始列表相同,只是某些梁的IsJoist属性将设置为true.

var result …
Run Code Online (Sandbox Code Playgroud)

c# linq list

6
推荐指数
2
解决办法
8855
查看次数

如何在 WPF 中更改 MouseOver 上的 MenuItem 的背景

阅读了很多东西并提出了很多问题,我终于为我的Menu/MenuItemin WPF.

但我被困在想要更改突出显示的单个子菜单的背景和边框的地方。

在下图中,我希望将蓝色背景更改为黄色!

在此输入图像描述

这是迄今为止完整的工作代码:

<Window x:Class="WpfApplication12.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>

        <LinearGradientBrush x:Key="DarkBrush" StartPoint="0.5,0" EndPoint="0.5,1">
            <GradientBrush.GradientStops>
                <GradientStopCollection>
                    <GradientStop Color="#939393" Offset="0.0"/>
                    <GradientStop Color="#717171" Offset="0.05"/>
                    <GradientStop Color="#606060" Offset="1.0"/>
                </GradientStopCollection>
            </GradientBrush.GradientStops>
        </LinearGradientBrush>

        <LinearGradientBrush x:Key="Clicked" StartPoint="0.5,0" EndPoint="0.5,1">
            <GradientBrush.GradientStops>
                <GradientStopCollection>
                    <GradientStop Color="#363636" Offset="0.0"/>
                    <GradientStop Color="#393939" Offset="1.0"/>
                </GradientStopCollection>
            </GradientBrush.GradientStops>
        </LinearGradientBrush>


        <Style x:Key="{x:Type Menu}" TargetType="Menu">
            <Setter Property="OverridesDefaultStyle" Value="True"/>
            <Setter Property="SnapsToDevicePixels" Value="True"/>
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Menu">
                        <Border x:Name="MainMenu" Background="#535353">
                            <StackPanel ClipToBounds="True" Orientation="Horizontal" IsItemsHost="True"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <ControlTemplate …
Run Code Online (Sandbox Code Playgroud)

c# wpf menu menuitem

6
推荐指数
1
解决办法
7052
查看次数

如何在WPF中将变量作为Converterparameter传递

我试图将后面的代码中定义的变量传递给ConverterParameter.我将在转换器中使用此参数然后决定某些单位转换.问题是我不知道如何通过这个.变量不是静态的.

<TextBox Text="{Binding MinimumRebarsVerticalDistance, Converter={StaticResource LengthConverter}, ConverterParameter={CurrentDisplayUnit}}"/>
Run Code Online (Sandbox Code Playgroud)

代码背后:

private Units currentDisplayUnit;
public Units CurrentDisplayUnit
{
    get { return currentDisplayUnit; }
    set
    {
        currentDisplayUnit = value;
        RaisePropertyChanged("CurrentDisplayUnit");
    }
}
Run Code Online (Sandbox Code Playgroud)

c# wpf

6
推荐指数
1
解决办法
7773
查看次数

在WPF中构建CAD程序

- 在WPF中构建CAD程序:

我想构建一个一次有10000个对象的CAD程序LINE.我正在使用LineGeomery类绘制添加到a的行Canvas.我已经实现了ZoomPan,到目前为止性能非常好.

只有一个主要的失望:

Thickness变焦时的线路被缩放.我已经尝试将线条BindThickness属性设置为保持它们不变的因素,这可以有效但在缩放时会显着降低性能.清除和绘制新厚度的新线条MouseWheel也是不可能的.这也会降低性能,并且在当前方法中不实用.

- 现在我有什么解决方案?

  • 坚持使用当前方法并忽略厚度的变化
  • 在GDI +中完成整个工作
  • WPF中的主机GDI
  • 使用WPF Viewport3D(LineThickness会在那里不变吗?)

- 其他解决方案?

您还需要采取哪些其他途径.我是WPF和编程的新手,我很想学习.

更新:

这就是我现在正在做的方式.我使用Pen画笔在可视层上绘制3000行.然后在MouseWheel事件上,我重新绘制所有具有更新厚度的线.此外,我不会向用户显示其余的线条,直到他缩放,所以我只在每个MouseWheel事件中创建10000条线中的3000条.

c# wpf performance zoom

5
推荐指数
1
解决办法
2200
查看次数

WPF C# 中的 StreamGeometry 与 DrawingContext.DrawLine

我将实时绘制数百条线。我选择了视觉层来做到这一点。但我看到这里有两种不同的画线方式。您建议使用哪一种以获得更好的性能和速度?

1.DrawingContext.DrawLine

public class DrawingTypeOne : FrameworkElement
{
    private readonly VisualCollection _visuals;
    public DrawingTypeOne(double thickness)
    {
        var myPen = new Pen
        {
            Thickness = 1,
            Brush = Brushes.White,
        };
        myPen.Freeze();

        _visuals = new VisualCollection(this);
        var drawingVisual = new DrawingVisual();
        using (var dc = drawingVisual.RenderOpen())
        {
            dc.DrawLine(myPen, new Point(0,0) , new Point(100,100));
            _visuals.Add(drawingVisual);
        }
    }

    protected override Visual GetVisualChild(int index)
    {
        return _visuals[index];
    }

    protected override int VisualChildrenCount
    {
        get
        {
            return _visuals.Count;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

2. …

c# wpf performance line drawingcontext

5
推荐指数
1
解决办法
4589
查看次数

在WPF中仅刷一个Ellipse的部分

我很难找到画出下面形状的最佳方法.我正在使用下面的代码绘制一个Ellipse可视图层.

但是我怎么才能刷四分之一?我认为它可以使用LinearGradientBrushRadialGradientBrush但我不知道如何使用它.

var cntrpoint = space.FlipYAxis(x2, y2);
dc.DrawEllipse(Brushes.Transparent, pen, cntrpoint, 30, 30);
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

c# wpf brush ellipse

5
推荐指数
1
解决办法
1330
查看次数

如何知道 OleDb 连接中的 Access 数据库中是否存在表

OleDb我使用下面的代码通过连接来连接到 Access 数据库C# .Net

我如何知道硬编码到程序中的表是否确实存在于文件中,以便我可以向用户显示适当的消息?

try
{
    var dbSource = "Data Source = " + source;
    const string dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;";

    using (var con = new OleDbConnection())
    {
        con.ConnectionString = dbProvider + dbSource;
        con.Open();

        using (var cmd = new OleDbCommand())
        {
            cmd.Connection = con;
            cmd.CommandText = "SELECT * FROM [Concrete Design Table 1]";

            // How do I know the table name is valid? It results in errors when it is not?
            // How to prevent it?
            using (var …
Run Code Online (Sandbox Code Playgroud)

.net c# oledb datareader oledbconnection

5
推荐指数
1
解决办法
6863
查看次数

使用C#中的LINQ获取List <T>中的不同属性值的数量

在列表中List<MyClass> ListOfMyClasses,如何GroupId使用LINQ 获取有多少不同的属性值?

public class MyClass
{
     public int GroupId;
}
Run Code Online (Sandbox Code Playgroud)

例如,假设我们有这个列表:

ListOfMyClasses: {MyClass1 (GroupId = 1), MyClass2 (GroupId = 3), MyClass3 (GroupId = 1)}
Run Code Online (Sandbox Code Playgroud)

这里我们应该得到结果为2(GroupId的两个不同的数字).

c# linq group-by list distinct

5
推荐指数
1
解决办法
1282
查看次数

在SkiaSharp中绘制旋转文本

如何绘制旋转文本SkiaSharp.

目前我正在旋转SKCanvas,绘制文本然后将其旋转回来.但我想可能有更有效的方法来做到这一点.

   canvas.RotateDegrees(45, 20, 20);
   canvas.DrawText("Text", 20, 20, paint);
   canvas.RotateDegrees(-45, 20, 20);
Run Code Online (Sandbox Code Playgroud)

wpf skia skiasharp

5
推荐指数
2
解决办法
1558
查看次数