2011-10-09 41 views
0

我想寫一個代表水箱的自定義silverlight控件。它有兩個依賴項屬性,liquidLevel和liquidCapacity,我想將這兩個參數和gradientBrush一起傳遞給一個轉換器。這個想法是,轉換器將根據液位和容量進行計算,並調整刷子上的梯度停止位置,以使液體上升和下降。訪問IvalueConverter |中的用戶控件屬性silverlight 4

我的坦克有一個「窗口」,這僅僅是一個矩形和gradientBrush,到目前爲止,我得到這個

我的控制模板

<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 * 

使用XAML控制(最終我想綁定這些性質)

 <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,但如果這是可能的,我不能弄清楚語法(我很新,silverlight)。

現在我已經有了這麼遠的想法我可以創建一個名爲fillpercentage的獨立屬性,並在最終的viewmodel中執行這個計算,但是數據將在那裏組織的方式非常肯定我會有一個整體如果我嘗試這個新的挑戰。對我來說,能夠對xaml中的液體容量進行硬編碼並將液位綁定到視圖模型似乎更易於管理。視圖模型將從數據庫中抽取一些值並將其轉換爲可觀察的字典,其中一個屬於液位級別,因此,我認爲將liquidlevel直接綁定到可觀察字典,而不是嘗試並轉換它綁定到視圖模型中的「fillpercentage」。

另外我很頑固,爲了我的教育興趣,有沒有人知道我是否有可能做到。如果是這樣,那麼正確的方式呢?

回答

0

當你說你

試圖通過liquidCapacity通過一個轉換器參數

我懷疑你想這樣做

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

此次榮獲」工作。你不能在另一個綁定中有綁定。

就個人而言,我不會使用轉換器來處理您要做的事情。相反,我會添加一個方法將LinearGradientBrush的漸變停止調整爲LiquidTank控件的代碼。然後,我會將PropertyChangedCallback s添加到LiquidTank控件的LiquidLevelLiquidCapacity依賴項屬性中,並在這些回調中調用此方法。

+0

歡呼,這個想法適合我 –

相關問題