2016-03-05 72 views
0

我在我的「ButtonStyles.xaml」資源字典中有一個橢圓形按鈕的模板。 這是部分代碼:動態改變橢圓從c#填充#

<SolidColorBrush x:Key="brush" Color="Red"/> 
<Style TargetType="Button" x:Key="MyButtonStyle"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate> 
       <Grid> 
        <VisualStateManager.VisualStateGroups> 
         <VisualStateGroup x:Name="CommonStates"> 
          <VisualState x:Name="Normal"> 
           <VisualState.Setters> 
            <Setter Target="ellipse.(Shape.Fill)" Value="{StaticResource ResourceKey=brush}"> 
            </Setter> 
            <Setter Target="ellipse.(Shape.Stroke)" Value="{x:Null}"/> 
           </VisualState.Setters> 
          </VisualState> 
          [...] 
         </VisualStateGroup> 
        </VisualStateManager.VisualStateGroups> 
        <Ellipse x:Name="ellipse"/> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

這裏是在對的MainPage按鈕:

<Button x:Name="toggle" Content="" HorizontalAlignment="Left" Height="132" Margin="40,146,0,0" VerticalAlignment="Top" Width="132" Style="{StaticResource MyButtonStyle}"/> 

現在我想刷資源從C#代碼改變。要做到這一點,我嘗試如下:

Application.Current.Resources["brush"] = new SolidColorBrush(Windows.UI.Color.FromArgb(255, 125, 126, 126)); 

雖然刷資源有效地改變橢圓的顏色沒有。我認爲問題是我不應該改變模板中的顏色,而是在實際的MainPage按鈕中。 這是問題嗎?我怎樣才能做到這一點?

+0

如果您想要在代碼中動態地更改填充,爲什麼要將其設置爲靜態資源?爲什麼不把它綁定到一個領域? –

+0

**這將幫助你以某種方式** [橢圓填充顏色與C#](http://stackoverflow.com/questions/2451326/wpf-binding-to-change-fill-color-of-ellipse?rq=1) – CodeConstruct

回答

0

我在@ felix-castor的建議和this指南中得到了一個結果。我在C#代碼中放置了DependencyProperty,在XAML代碼中放置了Binding。

它工作得很好,但是當屬性發生變化時它不會刷新按鈕,我必須在VisualState之間進行更改才能更新填充。我會發布關於它的另一個問題。但如果你有建議,你可以發表評論。