2014-02-20 3 views
1

Я создаю библиотеку управления в файле xaml (library.xaml), в котором я определил все элементы управления и im, обрабатывая их события в файле .cs с именем (library.cs) I хотите получить доступ дочерний элемент словаря ресурсов в .cs файле ... я ДНТ KNW, как сделать этоДоступ к дочернему элементу в библиотеке управления Xaml

Вот мои два файла

library.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       x:Class="custom_template.library"> 

<Style x:Key="my_progressbar_primary"> 
    <Setter Property="Control.Template"> 
    <Setter.Value> 
    <ControlTemplate TargetType="ProgressBar" > 
     <Grid Height="{TemplateBinding Height}" Width="{TemplateBinding Width}"> 
        <Rectangle HorizontalAlignment="Left" Height="20px" VerticalAlignment="Top" Width="{TemplateBinding Width}" Stroke="#ddd" ClipToBounds="True" RadiusX="3" RadiusY="3" Style="{StaticResource style_shadow_top}"></Rectangle> 
        <Rectangle Height="20" Width="100" HorizontalAlignment="Left" VerticalAlignment="Top" RadiusX="3" RadiusY="3" Style="{StaticResource style_shadow_top_primary}"></Rectangle> 
     </Grid> 
    </ControlTemplate> 
    </Setter.Value> 
    </Setter> 
</Style> 
</ResourceDictionary> 

library.cs

using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 
using System.Timers; 

    namespace custom_template 
    { 
    partial class library 
    { 
    private void xxx() 
    { 

    } 
    } 
} 

Проблема: я хочу изменить ширину прямоугольника в методе xxx() ..... но я не могу получить к нему доступ ... я также попытался получить доступ к прямоугольнику с помощью findname, но он не работает help меня

ответ

-1

Выпей имя прямоугольника,

<Rectangle x:Name="rect" HorizontalAlignment="Left" Height="20px" VerticalAlignment="Top" Width="{TemplateBinding Width}" Stroke="#ddd" ClipToBounds="True" RadiusX="3" RadiusY="3" Style="{StaticResource style_shadow_top}"></Rectangle> 

и сделайте следующее в библиотеке класса,

private void xxx() 
{ 
    if (Template != null) 
    { 
     var rect = (Rectangle)Template.FindName("rect", this); 
     //Where this is the Instance of ProgressBar. 
     //check the rect is not null and change the width of rect here... 
    } 
} 

Обновление:

In your `OnApplyTemplate` of your custom control.. 

protected override void OnApplyTemplate() 
{ 
    RectProperty = GetTemplateChild("rect") as Rectangle; 
    //RectProperty is your property 
} 
+0

я уже пробовал это, но я получаю сообщение об ошибке, что «имя шаблона не существует в текущем контексте». Есть ли другой способ получить к нему доступ? – Ravi

+0

Вы пытались контролировать? – Sankarann

+0

Как я упоминал ранее, я не хочу использовать обработчик событий и событий – Ravi

Смежные вопросы