簡體   English   中英

如何在WP 8.1中動態應用按鈕樣式?

[英]How to apply a style of a Button dynamically in WP 8.1?

我有一個WP 8.1應用程序。 我正在遵循完整的MVVM設計模式。 在XAML代碼中

 <Grid>
    <Canvas x:Name="gameCanvas"         
         Width="{Binding CanvasWidth, 
                 Mode=TwoWay, 
                 UpdateSourceTrigger=PropertyChanged}">
         <ItemsControl ItemsSource="{Binding Path=ButtonItems, 
                             Mode=TwoWay,                            
                             UpdateSourceTrigger=PropertyChanged}">
      <ItemsControl.ItemTemplate>
         <DataTemplate x:Name="Dta">
             <Button x:Name="newsItemBtn"
                 Height="{Binding Height}"
                 Width="{Binding Width}"
                 Style="{StaticResource ButtonStyle2}"
                 Content="{Binding Content}"
                 Command="{Binding                       
                           Path=DataContext.ButtonClickCommand,
                           ElementName=gameCanvas}"
                           CommandParameter="{Binding Content}">
             </Button>
          </DataTemplate>
       </ItemsControl.ItemTemplate>
    </ItemsControl>
 </Canvas>

但是我想要的是在運行時動態分配按鈕的樣式。 我在GamePage.XAML.定義了三個按鈕樣式GamePage.XAML.

<Style x:Key="ButtonStyle1" />
<Style x:Key="ButtonStyle2" />
<Style x:Key="ButtonStyle3" />

現在基於ViewModel某些值或某些屬性,我想選擇我嘗試使用converter的按鈕樣式。 但是沒有太大的成功。

知道我該如何實現嗎?

有一次我需要做同樣的事情,我只是使用了一個轉換器,它返回了一個命名資源:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    if (value is bool && (bool)value)
    {
        return App.Current.Resources["ComplexProductTypeTemplate"];
    }
    return App.Current.Resources["SimpleProductTypeTemplate"];
}

我猜想它可以與密鑰標識的資源以相同的方式工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM