小编Dr_*_*klo的帖子

wpf create style for named element

Is it possible to add style to xaml element without editing the element?

for example:

xaml element:

<Grid>
     <Grid x:Name="A">content A</Grid>
     <Grid x:Name="B">content B</Grid>
</Grid>
Run Code Online (Sandbox Code Playgroud)

and style:

<Style x:Key="StyleForA" TargetName="A" TargetType="{x:Type Grid}" >
   <Setter Property="Background" Value="Red"/>
</Style>
<Style x:Key="StyleForB" TargetName="B" TargetType="{x:Type Grid}" >
   <Setter Property="Background" Value="Green"/>
</Style>
Run Code Online (Sandbox Code Playgroud)

UPD: I have a project with a lot of styles (aero, black, etc ).

And if I edit the element Style="{StaticResources StyleForA}" I must edit all styles. So I need to create …

c# wpf xaml

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

使用TypeConverter将字符串转换为字符串数组

我需要转换的字符串"foo1,foo2,foo3"来string[].

我想用TypeConverter它或它的孩子ArrayConverter.它的包含方法ConvertFromString.

但是,如果我调用此方法,我会遇到异常 ArrayConverter cannot convert from System.String.

我知道Split,不建议我这个解决方案.

- - 解 - -

使用@Marc Gravell的建议和@Patrick Hofman的这个主题的回答我写道 CustumTypeDescriptorProvider

public class CustumTypeDescriptorProvider:TypeDescriptionProvider
    {
        public override ICustomTypeDescriptor GetTypeDescriptor(System.Type objectType, object instance)
        {
            if (objectType.Name == "String[]") return new StringArrayDescriptor();
            return base.GetTypeDescriptor(objectType, instance);
        }
    }
public class StringArrayDescriptor : CustomTypeDescriptor
    {
        public override TypeConverter GetConverter()
        {
            return new StringArrayConverter();
        }
    }
Run Code Online (Sandbox Code Playgroud)

在StringArrayConverter这篇文章下面的答案中实现了哪里.为了使用它,我添加CustumTypeDescriptorProvider了提供者集合

 TypeDescriptor.AddProvider(new CustumTypeDescriptorProvider(), typeof(string[]));
Run Code Online (Sandbox Code Playgroud)

要在它中使用它,TestClass …

c# string converter

0
推荐指数
1
解决办法
1752
查看次数

标签 统计

c# ×2

converter ×1

string ×1

wpf ×1

xaml ×1