locked
实现xaml中类似于LinerGradientBrush中直接写GradientStop类型的子项 RRS feed

  • 问题

  • 在xaml定义渐进色刷的时候如下

    <LinerGradientBrush>

    <GradientStop Color='red' offset='0' />

    <GradientStop Color='Yellow offset='1' />

    </LinerGradientBrush>

    分析了一下所有GradientStop 均是 LinerGradientBrush 一个GridientStopCollection类型一个属性的值。

    GridientStopCollection 则是继承 PresentationFrameworkCollection<GradientStop>

    现想实现如渐进色刷那样的在xaml那个设置的格式。(无法继承PresentationFrameworkCollection<T> 其抽象方法为internal访问权限,格式一定要向下面一样,不能继承ItemsControl ,那样显得很臃肿每一个都要声明一个ItemsControl。我的数据结构可能是一个树形的。)

    比如:

    <Mydatas>

    <Data id='1' value='0' >

        <Data id='1.1' value='2'/>

        <Data id='1.2' value='4'/>

    </Data>

    <Data id='2 value='10' />

    </Mydatas>

    该如何实现

    2011年1月21日 7:28

答案

  • 谢谢你的答复。 将Content属性指向List<Data> 试了会报 PresentationFrameworkCollection<T> 中未实现的方法的错误。 偶尔看到一段代码。给了灵感。可以达到。先共享出来。 public class Data :List<Data> { } 这样就可以形成树结构的了。 xaml中 <Data > <Data id='1' value='0' > <Data id='1.1' value='2'/> <Data id='1.2' value='4'/> </Data> <Data id='2' value='0' > </Data> </Data> cs代码中。直接可以。 foreach(Data d in data) { }
    • 已标记为答案 worldman 2011年1月28日 12:10
    2011年1月28日 12:09

全部回复

  • 你好,

    你可以通过使用ContentPropertyAttribute 来隐藏类属性

    http://msdn.microsoft.com/en-us/library/system.windows.markup.contentpropertyattribute(VS.95).aspx

    比如Panel

    [ContentProperty("Children", true)]
    public abstract class Panel : FrameworkElement
    {
        // Fields
        public static readonly DependencyProperty BackgroundProperty;
        private static readonly DependencyProperty ChildrenProperty;
        public static readonly DependencyProperty IsItemsHostProperty;

        // Methods
        static Panel();
        protected Panel();
        internal Panel(uint nKnownTypeIndex);

        // Properties
        public Brush Background { get; set; }
        public UIElementCollection Children { get; }
        public bool IsItemsHost { get; }
    }

     


    Mog Liang
    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework
    • 已建议为答案 Mog Liang 2011年1月28日 9:07
    2011年1月24日 6:20
  • 谢谢。那些数据类直接继承于 DepentyObject即可直接写在xaml中。明白了。
    2011年1月24日 15:11
  • 你好,这样虽然可以解决内容属性的问题,但是其内容属性的类型无法自定义。即对应到上面代码中的

    UIElementCollection  其实我的问题是想实现一个类似于 UIElementCollection  这样一个类

    2011年1月25日 2:33
  • 若你想实现可以迭代多层的<Data>,那么就让Content指向List<Data>,这样可以吗?


    Mog Liang
    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework
    2011年1月26日 9:18
  • 谢谢你的答复。 将Content属性指向List<Data> 试了会报 PresentationFrameworkCollection<T> 中未实现的方法的错误。 偶尔看到一段代码。给了灵感。可以达到。先共享出来。 public class Data :List<Data> { } 这样就可以形成树结构的了。 xaml中 <Data > <Data id='1' value='0' > <Data id='1.1' value='2'/> <Data id='1.2' value='4'/> </Data> <Data id='2' value='0' > </Data> </Data> cs代码中。直接可以。 foreach(Data d in data) { }
    • 已标记为答案 worldman 2011年1月28日 12:10
    2011年1月28日 12:09