小编Grx*_*x70的帖子

TypeConverter行为不一致?

我正在开发一个IValueConverter可以转换bool?值的实现.为了多功能性,我决定使用TypeConverter将输入值转换为bool?.由于它的主要目的是用作XAML绑定的转换器,因此我希望避免抛出异常,因为它会导致UI性能的显着降低.为此,我尝试使用TypeConverter.IsValid方法,但遇到了特殊的行为,其示例在以下代码中显示:

//returned converter is a NullableConverter
var converter = TypeDescriptor.GetConverter(typeof(bool?));

//this method returns false
converter.IsValid(string.Empty);

//yet this method returns null without throwing an exception
converter.ConvertFrom(string.Empty);
Run Code Online (Sandbox Code Playgroud)

也许我错了,但是我希望只要一个值无法转换就会IsValid返回该方法false,true否则,但显然不是空字符串的情况NullableConverter(对于其他可以为空的类型可以观察到相同的行为).

这是一个错误还是设计选择?如果是后者,还有其他类似案例吗?

编辑

在检查源代码后,NullableConverter我想我已经找到了这种行为的原因.这是IsValid实施:

public override bool IsValid(ITypeDescriptorContext context, object value) {
    if (simpleTypeConverter != null) {
        object unwrappedValue = value;
        if (unwrappedValue == null) {
            return true; // null is …
Run Code Online (Sandbox Code Playgroud)

c# typeconverter

17
推荐指数
1
解决办法
1075
查看次数

剪切到WPF中的路径

我试图在WPF中创建一个用户控件,允许用户选择鞋子的特定区域(鞋跟,边缘,鞋底等)

这个想法是你有一个鞋子的图像(绘图),你可以点击单个部分并选择区域.

我正在使用一组模板化的复选框.

有一个复选框,其中包含定义边的路径,然后是一组定义各个区域的矩形.

鞋印

这很好用,除非它看起来不太好看.为了让它看起来更好,我想隐藏不在原始鞋道内的所有东西.

矩形全部位于网格中的单独行中,背景鞋跨越所有行.

我试图将父网格的Clip属性设置为与背景鞋边相同的路径,但得到了一些奇怪的结果:

在此输入图像描述

我很确定我的网格剪辑是正确的,但我不明白这里发生了什么.

如果有人可以帮助解决这个问题或建议更好的方法来完成同样的任务,我将不胜感激.

<Geometry x:Key="ShoeEdgeGeometry">M26.25,0.5 C40.471332,0.5 52,17.625107 52,38.75 52,51.292905 47.935695,62.425729 41.656635,69.401079 L41.349452,69.733874 42.012107,70.457698 C45.421829,74.364614 47.5,79.554564 47.5,85.25 47.5,97.400265 38.042015,107.25 26.375,107.25 14.707984,107.25 5.2499995,97.400265 5.2499991,85.25 5.2499995,79.554564 7.3281701,74.364614 10.737891,70.457698 L11.276058,69.869853 10.843364,69.401079 C4.5643053,62.425729 0.49999952,51.292905 0.5,38.75 0.49999952,17.625107 12.028667,0.5 26.25,0.5 z</Geometry>



 <Grid Margin="0"
          Clip="{StaticResource ShoeEdgeGeometry}">
        <Grid.RowDefinitions>
            <RowDefinition Height="2*" />
            <RowDefinition Height="4*" />
            <RowDefinition Height="2*" />
            <RowDefinition Height="2*" />
            <RowDefinition Height="2*" />
        </Grid.RowDefinitions>
        <!-- The edge check box-->
        <CheckBox x:Name="ShoeEdgeRegion"
                  Grid.Row="0"
                  Grid.RowSpan="5">
            <CheckBox.Style>
                <Style TargetType="CheckBox">
                    <Setter Property="Cursor"
                            Value="Hand" />
                    <Setter Property="Template">
                        <Setter.Value> …
Run Code Online (Sandbox Code Playgroud)

c# wpf xaml

11
推荐指数
2
解决办法
1748
查看次数

XML文档中的引用运算符

我想在<see cref="..." /> XML文档标记中引用一个运算符,但我似乎无法找到有关如何执行此操作的任何提示.此标记上的MSDN文章仅显示引用方法的简单示例,但不介绍可以引用的不同类型的成员.

特别是,我想引用一个隐式转换运算符,但是也可以理解引用运算符的一般规则.


比方说,我们有一个简单的结构,我们将其定义==,!=并且隐式转换操作符:

public struct MyStructure
{
    public int Value { get; set; }

    public static bool operator ==(MyStructure x, MyStructure y) => x.Value == y.Value;

    public static bool operator !=(MyStructure x, MyStructure y) => x.Value != y.Value;

    public static implicit operator MyStructure(int i) => new MyStructure { Value = i };
}
Run Code Online (Sandbox Code Playgroud)

只需足够的人可以引用该Value属性<see cref="MyStructure.Value" />,但如何引用==运算符?我显然试图<see cref="MyStructure.==" />和 …

.net c# xml-documentation

11
推荐指数
2
解决办法
1077
查看次数

C# WPF 非常简单的 TextBox 验证

我是 C# 的初学者.... 我想为 TextBox 创建一个非常简单的验证。到目前为止,这是我的代码:

主窗口.xaml.cs

namespace Mynamespace
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void TextBox_TextChanged_2(object sender, TextChangedEventArgs e)
        {
        }
    }

    public class AgeValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            int wert = Convert.ToInt32(value);
            if (wert < 0)
                return new ValidationResult(false, "just positiv values allowed");
            return new ValidationResult(true, null);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

主窗口.xaml

<Window x:Class="Mynamespace.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Mynamespace"
        Title="MainWindow"
        Height="350"
        Width="525">
<Window.Resources>
</Window.Resources>
<Grid>
    <TextBox …
Run Code Online (Sandbox Code Playgroud)

c# validation wpf xaml textbox

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

标签 统计

c# ×4

wpf ×2

xaml ×2

.net ×1

textbox ×1

typeconverter ×1

validation ×1

xml-documentation ×1