2011-10-09 2 views
0

물 탱크를 나타내는 사용자 지정 실버 라이트 컨트롤을 작성하려고합니다. 그것은 두 가지 의존성 속성 인 liquidLevel과 liquidCapacity를 가지고 있습니다. 그리고 두 매개 변수를 gradientBrush와 함께 변환기에 전달하고 싶습니다. 아이디어는 변환기가 액체 레벨과 용량을 기반으로 계산을 수행하고 브러시의 기울기 조정을 조정하여 액체의 상승 및 하강을 나타냅니다.IvalueConverter에서 사용자 정의 컨트롤 속성에 액세스하기 | silverlight 4

내 탱크는 지금까지 내가 XAML에서 컨트롤을 사용하여이

내 컨트롤 템플릿

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:MoreControls;assembly=MoreControls" 
    xmlns:assets="clr-namespace:MoreControls.Assets"> 

    <LinearGradientBrush x:Name="LiquidLevelTankWindow" StartPoint="0.469,0.997" EndPoint="0.487,0.013"> 
     <GradientStop Color="#FF1010F1" Offset="0.0"/> 
     <GradientStop Color="#FF5555FB" Offset="0.55"/> 
     <GradientStop Color="#FFE4E4F1" Offset="0.6"/> 
     <GradientStop Color="#FFFAFAFD" Offset="1"/> 
    </LinearGradientBrush> 

    <assets:LiquidLevelBrushConverter x:Name="LiquidLevelBrushConverter" levelBrush="{StaticResource LiquidLevelTankWindow}"/> 

    <Style x:Key="Liquid" TargetType="local:LiquidTank"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="local:LiquidTank"> 

       // * parts of the control here * 

       // the part of the control im interested in 
       <Rectangle x:Name="TankWindow" Width="32.3827" Height="64" Canvas.Left="27" Canvas.Top="42" Stretch="Fill" StrokeLineJoin="Round" Stroke="#FF000310" 
            Fill="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=LiquidLevel, Converter={StaticResource LiquidLevelBrushConverter}}" /> 

       // * rest of control template * 

가지고있다, 단지 직사각형 gradientBrush있는 "창"을 가지고 (결국 내가 바인딩 할 이러한 속성)

 <local:LiquidTank Style="{StaticResource Liquid}" LiquidCapacity="100" LiquidLevel="50"/> 

및 컨버터

public class LiquidLevelBrushConverter : IValueConverter 
    { 
     public LinearGradientBrush levelBrush { get; set; } 

     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
     //I can access the liquid level parameter here   
      double level = 0; 
      double.TryParse(value.ToString(), out level); 

      GradientStopCollection gsc = levelBrush.GradientStops; 
      //some logic to alter gradient stops 

      return null; 
     } 

지금 나는 전체 변환기의 비율을 계산할 수 있도록 변환기에서 두 번째 제어 속성 인 liquidCapacity에 액세스하려고합니다. 나는 liquidCapacity를 변환기 매개 변수로 통과 시키려고 시도했지만 가능한 경우 구문을 이해할 수 없다 (나는 실버 라이트에 익숙하지 않다).

필자는 필자가 fillpercentage라는 단일 종속 속성을 만들 수 있었고 궁극적 인 뷰 모델에서이 계산을 수행 할 수 있다고 생각 했으므로 데이터가 정리 될 것입니다. 이 새로운 시도를 시도해보십시오. xaml에서 액체 용량을 하드 코드하고 뷰 모델에 액체 레벨을 바인딩 할 수있게하는 것이 더 관리하기 쉽습니다. 뷰 모델은 데이터베이스에서 많은 가치를 끌어내어 관측 가능한 사전 (liquidlevel) 중 하나 인 관측 가능한 사전에 넣을 것이므로 액체 레벨을 관찰 가능한 사전에 직접 바인딩하는 것이 훨씬 쉽습니다. 그것을 바인딩하기 전에 뷰 모델의 "채움 비율"로 변경하십시오.

플러스 임 꽤 고집스럽고 내 교육에 관심이있어서 내가 제안한 것이 가능하면 누구든지 알 수 있습니다. 그렇다면 올바른 방법은 무엇입니까?

답변

0

당신은 당신이

가 변환 매개 변수

난 당신이

<Rectangle Fill="{Binding Path=LiquidLevel, ConverterParameter={Binding Path=LiquidCapacity} ...}" /> 

이 원 '그런 짓을하려는 생각으로 통해 liquidCapacity을 전달하려했다고 말할 때 일하지 마라. 다른 바인딩 내부에 바인딩을 가질 수 없습니다.

개인적으로, 나는 개인적으로 당신이하려는 것을 위해 변환기를 사용하지 않을 것입니다. 대신 LinearGradientBrush의 그래디언트 멈춤 점을 LiquidTank 컨트롤의 코드로 조정하는 메서드를 추가했습니다. 그런 다음 LiquidTank 컨트롤의 LiquidLevelLiquidCapacity 종속성 속성에 PropertyChangedCallback을 추가하고 이러한 콜백 내에서이 메서드를 호출합니다.

+0

그 아이디어가 저에게 효과적입니다. –

관련 문제