Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > WPF и Silverlight > Создание контрола с дополнительными свойствами


Автор: makaka 19.7.2011, 14:56
Нужно агрегировать часто используемую разметку XAML в один контрол. У него нужно создать дополнительные свойства, которых нет в UserControl

У меня такой код:
Код

<UserControl x:Class="ruTender.PriceRangeSelector2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    
    <Grid x:Name="LayoutRoot" Background="White">
        <StackPanel Orientation="Horizontal">
            <TextBox Width="100"
                     Text="{TemplateBinding MinPrice}"/>
            <TextBox Width="100"
                     Text="{TemplateBinding MaxPrice}" />
            <ComboBox x:Name="CurrencyCombobox">
            </ComboBox>
        </StackPanel>
    </Grid>
</UserControl>


Код

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace ruTender {

    [TemplatePart(Name = "MinPrice", Type = typeof(string))]
    [TemplatePart(Name = "MaxPrice", Type = typeof(string))]
    public partial class PriceRangeSelector2 : UserControl {
        public PriceRangeSelector2() {
            InitializeComponent();
        }

        #region Dependency Properties
        public static  DependencyProperty MinPriceProperty =
                        DependencyProperty.Register("MinPrice",
                                         typeof(string),
                                         typeof(PriceRangeSelector2),
                                         new PropertyMetadata("0"));

        public static  DependencyProperty MaxPriceProperty =
                        DependencyProperty.Register("MaxPrice",
                                         typeof(string),
                                         typeof(PriceRangeSelector2),
                                         new PropertyMetadata("0"));
        #endregion

        #region Public Properties
        public string MinPrice {
            get { return (string)GetValue(MinPriceProperty); }
            set { SetValue(MinPriceProperty, value); }
        }

        public string MaxPrice {
            get { return (string)GetValue(MaxPriceProperty); }
            set { SetValue(MaxPriceProperty, value); }
        }
        #endregion
    }
}


При отображении страницы вылезает ошибка XAMLPArseException
и пишется "Не удалось создать "System.Windows.DependencyProperty" на основе текста "MinPrice"."

Что я неправильно сделал?


PS Тот же самый код прекрасно работает, если сделать его в качестве Templated Control
(соответственно XAML разметка выносится в Style)
Но к сожалению использовать Templated Control не очень удобно - мне желательно инициализировать список Combobox из кода c#

В общем я был бы очень признателен за подсказку как в обычном userControl сделать дополнительные свойства

Автор: makaka 21.7.2011, 14:44
вот так это делается. Просто, даже очень ))

http://forums.silverlight.net/p/22766/232430.aspx

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)