繁体   English   中英

从XAML实例化UIElement

[英]Instanciate UIElement from XAML

我想要达到的目标听起来并不像火箭科学。 我要创建的是一个自定义控件,我可以直接从XAML向其中传递UIElements项列表,以便每个元素可以不同并嵌入不同的对象(网格/文本框/面板等)。

这是我要使用的xaml代码:

    <wpf:TileListDoubleItem>
        <wpf:TileListDoubleItem.FrontItem>
            <Grid>
                <TextBlock FontFamily="Calibri,Verdana" FontSize="16" FontWeight="Bold" Foreground="White" Text="Hello"></TextBlock>
            </Grid>
        </wpf:TileListDoubleItem.FrontItem>
        <wpf:TileListDoubleItem.BackItem>
            <Grid>
                <TextBlock FontFamily="Calibri,Verdana" FontSize="16" FontWeight="Bold" Foreground="White" Text="World"></TextBlock>
            </Grid>                                 
        </wpf:TileListDoubleItem.BackItem>
    </wpf:TileListDoubleItem>

这是我的自定义控制代码:

    public partial class TileListDoubleItem : UserControl, INotifyPropertyChanged
    {
        private bool _flipped;
        internal bool CanFlip { get { return true; } }

        private bool flipped
        {
            get {
                return this._flipped;
            }
            set {
                this._flipped = value;
                DisplayItem = this._flipped ? BackItem : FrontItem;
            }
        }

        public ObservableCollection<TileSide> Sides { get; set; }
        public ICommand FlipCommand;

        public TileListDoubleItem()
        {
            InitializeComponent();
            FlipCommand = new FlipCommand(this);
            flipped = false;
        }

        private UIElement displayItem { get; set; }
        public UIElement DisplayItem
        {
            get { return this.displayItem; }
            set {
                if (this.displayItem != value)
                {
                    this.displayItem = value;
                    OnPropertyChanged("DisplayItem");
                }
            }
        }

        public void Flip()
        {
            try
            {
                flipped = !flipped;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public UIElement FrontItem
        {
            get { return (UIElement)GetValue(FrontItemProperty); }
            set { SetValue(FrontItemProperty, value); }
        }

        public static readonly DependencyProperty FrontItemProperty =
        DependencyProperty.Register("FrontItem", typeof(UIElement), typeof(TileListDoubleItem), new UIPropertyMetadata(null));


        public UIElement BackItem
        {
            get { return (UIElement)GetValue(BackItemProperty); }
            set { SetValue(BackItemProperty, value); }
        }

        public static readonly DependencyProperty BackItemProperty =
        DependencyProperty.Register("BackItem", typeof(UIElement), typeof(TileListDoubleItem), new UIPropertyMetadata(null));

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(String propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

当我运行它时,我的FrontItem和BackItem都等于null,并且从不设置为UIElement(在此示例中为Grid)。 我想我所缺少的对于某些人来说必须是非常明显的。

在此先感谢任何人的帮助。

您的属性已按预期设置。 您可以通过创建两个绑定到控件属性的TextBlocks来确认这一点:

<wpf:TileListDoubleItem x:Name="control">
    <wpf:TileListDoubleItem.FrontItem>
        <Grid>
            <TextBlock FontFamily="Calibri,Verdana" FontSize="16" FontWeight="Bold" Foreground="White" Text="Hello"></TextBlock>
        </Grid>
    </wpf:TileListDoubleItem.FrontItem>
    <wpf:TileListDoubleItem.BackItem>
        <Grid>
            <TextBlock FontFamily="Calibri,Verdana" FontSize="16" FontWeight="Bold" Foreground="White" Text="World"></TextBlock>
        </Grid>
    </wpf:TileListDoubleItem.BackItem>
</wpf:TileListDoubleItem>

<TextBlock Text="{Binding FrontItem.Children[0].Text, ElementName=control}" />
<TextBlock Text="{Binding BackItem.Children[0].Text, ElementName=control}" />

显然,在TileListDoubleItem的构造方法返回时不会设置属性。 XAML解析器需要实例化该对象,然后才能设置其任何属性,就像您自己创建一个类的实例一样。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM