2015-11-01 109 views
0

我已經添加了一個新的屬性到ProgresBar控件,我怎麼不認爲我做得正確。下面是MainWindow.xaml中的進度條,我需要2個值之間的差距。WPF自定義屬性

<ProgressBar Style="{StaticResource CircularProgress}" 
       Value="50" 
       Extensions:CustomExtensions.Radius="140 0" /> 

現在,這裏是我的自定義擴展,由於兩個數字之間存在差距,所以我將它定義爲一個字符串。

public static readonly DependencyProperty RadiusProperty = 
    DependencyProperty.RegisterAttached("Radius", typeof(string), typeof(CustomExtensions), new PropertyMetadata(default(string))); 

public static void SetRadius(UIElement element, string value) 
{ 
    element.SetValue(RadiusProperty, value); 
} 

public static string GetRadius(UIElement element) 
{ 
    return (string)element.GetValue(RadiusProperty); 
} 

現在,我在這裏使用這個自定義屬性,這是不工作。

<PathFigure x:Name="pathFigure" StartPoint="{Binding Path=Radius, RelativeSource={RelativeSource TemplatedParent}}"> 

真的我有2個問題:1。 值似乎並沒有被應用到我的控件模板,因爲如果我刪除綁定的輸入140 0自己這顯示了ArcSegment,但與它結合沒有。

  1. 是否有可能只鍵入Radius的定製屬性沒有Extensions:CustomExtensions

編輯: 當試圖綁定的文本框爲這個值我得到這個錯誤:

Exception thrown: 'System.Windows.Markup.XamlParseException' in PresentationFramework.dll

Additional information: 'Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.' Line number '36' and line position '20'.

代碼:

<ProgressBar Style="{StaticResource CircularProgress}" 
        Value="{Binding Source={StaticResource runtimeVariables},Path=uploadProgress}" 
        Extensions:CustomExtensions.Radius="80" 
        Name="test"/> 
     <TextBlock Text="{Binding ElementName=test, Path=(Extensions:CustomExtensions.Radius)}"/> 
+1

對我來說,與元素,只有「半徑」結合的道路是工作(如我貼在改變附加屬性後,我的答案),所以如果錯誤不是來自這個,我想它可能來自上一個綁定(嘗試沒有它 - >給你的第一個例子硬編碼值) – kirotab

回答

1

你必須,如果結合到附加屬性使用特殊的語法(用括號圍繞附屬物)。此外,你應該指定一個轉換器將字符串轉換爲Point。

下面是一個例子:

<PathFigure x:Name="pathFigure" StartPoint="{Binding Path=(Extensions:CustomExtensions.Radius), RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource pointConverter}"> 

編輯: 我無法重現您的問題。 我用下面的代碼和文本塊有正確的輸出:

<ProgressBar Extensions:CustomExtensions.Radius="80" Name="test"/> 
<TextBlock Text="{Binding ElementName=test, Path=(Extensions:CustomExtensions.Radius)}"/> 
+0

感謝更多的問題請參閱編輯。 –

+0

@MartynBall查看我的編輯。 – Stipo

1

我錯在我原來的答覆中,所附屬性的類型被正確定義,應該是CustomExtensions而不是ProgressBar

ownerType - The owner type that is registering the dependency property. MSDN Reference

你必須設置進度的附加屬性typeof(ProgressBar)

public static readonly DependencyProperty RadiusProperty = 
    DependencyProperty.RegisterAttached(
     "Radius", typeof(string), 
     typeof(CustomExtensions), 
     new PropertyMetadata(default(string)) 
    ); 

這裏是你如何可以輕鬆地測試

<ProgressBar x:Name="testProgressBar" 
      Value="50" 
      local:CustomExtensions.Radius="140 0" /> 
<TextBlock Text="{Binding ElementName=testProgressBar, Path=Radius}"/>