2013-05-07 32 views
0

我想創建一個Style作爲窗口資源,並且該樣式需要綁定到分配的控件中的屬性。下面是它的簡化示例。帶分配控件的樣式數據綁定

使用分配的按鈕控制標籤屬性,爲按鈕創建樣式並應用Background顏色。

<Window.Resources> 
    <Style x:Key="TestingStyle" TargetType="Button"> 
     <Setter Property="Background" Value="{Binding Tag}" /> 
    </Style> 
</Window.Resources> 

當我在Tag添加了Color一個Button,這種風格應該應用顏色Button的背景。這是可能的嗎?

編輯

下面是實際XMAL代碼。

<Style x:Key="SeriesStyle" TargetType="Chart:ChartSeries"> 
    <Setter Property="StrokeThickness" Value="2"/> 
    <Setter Property="PointMarkerTemplate"> 
     <Setter.Value> 
      <ControlTemplate> 
       <Ellipse Width="7" Height="7" Fill="Lavender" Stroke="{Binding RelativeSource={RelativeSource Self}, Path=SeriesColor}"/> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
+0

發生了什麼,它只是一個工作或者是崩潰? – Heinrich 2013-05-07 22:56:29

+0

什麼是SeriesColor,它是ChartSeries的一個屬性?因爲如果是這種情況,那麼RelativeSource Self不一定會工作。請在下面檢查我的答案,並在其中添加一些內容 – Heinrich 2013-05-07 23:01:39

+0

@ Heinrich:當我使用這種風格時,它不會做任何事情(不會崩潰)。 – ABCD 2013-05-07 23:20:21

回答

2

是有可能做到這一點,但是你必須綁定的RelativeSource,以及可能不必創建該對象轉換爲顏色的轉換,這是因爲標籤存儲的對象沒有顏色,下面的 是添加相對來源的示例。

<Setter Property="Background" Value="{Binding Path=Tag, RelativeSource={RelativeSource Self}}}" /> 

編輯:
假設系列顏色是ChartSeries中物業使用:

{Binding Path=SeriesColor, RelativeSource={RelativeSource AncestorType={x:Type Chart:ChartSeries}}} 
+0

聽起來像一個解決方案,但沒有奏效。可能是我犯了一個錯誤。請問你看到問題中的編輯?謝謝 – ABCD 2013-05-07 22:47:30

+0

仍然沒有工作(風格不適用)。感謝您展示方向。 – ABCD 2013-05-07 23:21:29

+0

Mhmmm另一個想法PointMarker本身就是一個UIElement,不是嗎?所以當你使用上面的方法時,你正在尋找Parent它會找到PointMarker,所以嘗試添加「Path = Parent.SeriesColor」改變「AncestorType = {x:Type Chart:PointMarker}」,我現在只是在釣魚我不知道什麼ChartSeries是如此,我只是繼續我期望的情況下。 – Heinrich 2013-05-07 23:49:01

1

也許試試這個:

<Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Self}, Path=Tag}" /> 
+0

+1,謝謝你的回答! – ABCD 2013-05-08 01:01:31