2011-04-14 3 views
2

아주 간단한 lookless 컨트롤을 사용하고 있으며, 템플릿 바인딩 중 하나를 사용할 수 없습니다. 컨트롤에는 두 개의 종속성 속성이 있습니다. 하나는 문자열이고 int는 하나는 아닙니다.Silverlight databind int가 보이지 않는 컨트롤이 작동하지 않습니다.

는 CSHARP 코드는 다음과 같습니다

using System; 
using System.Windows; 
using System.Windows.Controls; 

namespace ControlDemo 
{ 
    public class TextControlLookless : Control 
    { 
     #region Title 

     public static readonly DependencyProperty ChartTitleProperty = 
      DependencyProperty.Register("ChartTitle", typeof(string), typeof(TextControlLookless), 
      null); 


     public String ChartTitle 
     { 
      get { return (string)GetValue(ChartTitleProperty); } 
      set 
      { 
       SetValue(ChartTitleProperty, value); 
      } 
     } 

     #endregion 

     #region Value 

     public static readonly DependencyProperty ChartValueProperty = 
      DependencyProperty.Register("ChartValue", typeof(int), typeof(TextControlLookless), 
      null); 


     public int ChartValue 
     { 
      get { return (int)GetValue(ChartValueProperty); } 
      set 
      { 
       SetValue(ChartValueProperty, value); 
      } 
     } 

     #endregion 

     #region ctor 

     public TextControlLookless() 
     { 
      this.DefaultStyleKey = typeof(TextControlLookless); 
     } 

     #endregion 

    } 
} 

그리고 컨트롤의 XAML은 다음과 같습니다

<ResourceDictionary 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="clr-namespace:ControlDemo"> 

<Style TargetType="local:TextControlLookless"> 
    <Setter Property="ChartTitle" Value="Set Title" /> 
    <Setter Property="ChartValue" Value="1" /> 

    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="local:TextControlLookless"> 
       <Grid x:Name="Root"> 
        <Border BorderBrush="Black" BorderThickness="2"> 
         <Grid> 
          <Grid.RowDefinitions> 
           <RowDefinition Height="Auto" /> 
           <RowDefinition Height="Auto" /> 
           <RowDefinition /> 
          </Grid.RowDefinitions> 
          <TextBlock Text="{TemplateBinding ChartTitle}" /> 
          <TextBlock Text="{TemplateBinding ChartValue}" Grid.Row="1" /> 
         </Grid> 
        </Border> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

내가 페이지에이를 넣어, 내가 볼 수 ChartTitle (Set Title 또는 내가 설정 한 것)이지만 ChartValue는 결코 나타나지 않습니다. 그 유형을 문자열로 변경하면, 그 문자열이 나타나기 때문에, 뭔가 빠져 있어야합니다.

답변

0

TemplateBindingBinding보다 훨씬 원시 연산입니다. Binding은 실제 클래스이며 다른 데이터 유형간에 문자열을 암시 적으로 변환하는 등의 유용한 기능을 포함합니다.

TemplateBinding은 마크 업 명령어이며 결정적으로 사용자를 위해 유형 변환을 수행하지 않습니다. 따라서 TextBlockText 속성에 바인딩되는 종속성 속성은 문자열이어야합니다.

선택 대신 TemplateBinding은 TextBlock 이름을 지정하고 지정 사용

하나 그것 ChartValue 속성 Text가 다시 전화 변경 : - -이 :

당신은 두 가지 선택이

#region Value 

    public static readonly DependencyProperty ChartValueProperty = 
     DependencyProperty.Register("ChartValue", typeof(int), typeof(TextControlLookless), 
     new PropertyMetadata(0, OnChartValuePropertyChanged)); 

    private static void OnChartValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     TextControlLookless source = d as TextControlLookless; 
     source.Refresh(); 
    } 


    public int ChartValue 
    { 
     get { return (int)GetValue(ChartValueProperty); } 
     set 
     { 
      SetValue(ChartValueProperty, value); 
     } 
    } 

    #endregion 

    private TextBlock txtChartValue { get; set; } 

    public override void OnApplyTemplate() 
    { 
     base.OnApplyTemplate(); 
     txtChartValue = GetTemplateChild("txtChartValue") as TextBlock; 
     Refresh(); 
    } 

    private void Refresh() 
    { 
     if (txtChartValue != null) 
     { 
      txtChartValue.Text = ChartValue.ToString(); 
     } 
    } 

XAML의 모습 : -

<TextBlock x:Name="txtChartValue" Grid.Row="1" /> 

다른 값은 값에 대한 개인 종속성 속성을 만드는 것입니다 문자열의 유형 전자 - XAML의 모습

 #region Value 

     public static readonly DependencyProperty ChartValueProperty = 
      DependencyProperty.Register("ChartValue", typeof(int), typeof(TextControlLookless), 
      new PropertyMetadata(0, OnChartValuePropertyChanged)); 

     private static void OnChartValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
     { 
      d.SetValue(ChartValueStrProperty, e.NewValue.ToString()); 
     } 

     private static readonly DependencyProperty ChartValueStrProperty = 
      DependencyProperty.Register("ChartValueStr", typeof(string), typeof(TextControlLookless), 
      new PropertyMetadata("0")); 

     public int ChartValue 
     { 
      get { return (int)GetValue(ChartValueProperty); } 
      set 
      { 
       SetValue(ChartValueProperty, value); 
      } 
     } 

     #endregion 

: - ChartValueStrProperty 개인이며, 나는 그것을 커버하기 위해 표준 .NET 속성을 만드는 귀찮게하지 않았 음을

 <TextBlock Text="{TemplateBinding ChartValueStr}" Grid.Row="1" /> 

참고. TemplateBinding 실제로 접미어를 할당 한 속성 이름이 "Property"인 경우 대상 유형에서 정적 필드를 찾습니다.

두 가지 방법 모두 장점과 약점이 있습니다. 첫 번째 방법은 더 일반적인 패턴이지만 조금 더 많은 코드를 사용하고 유연성이 떨어집니다 (값을 표시하는 컨트롤은 TextBlock이어야 함). 두 번째는 더 유연하며 코드를 적게 사용하지만 다소 정통 적이 지 않습니다.

+0

두 번째 작품이 마음에 들지만, 내 머리 속에서 더 잘 흐른다. –

관련 문제