![](/img/trans.png)
[英]play two sounds simultaneously in Windows 8 Metro App (C#/XAML)
[英]Windows 8 metro app: styling a button dynamically. C# and Xaml
我有一個帶有Gridview的xaml頁面和一個Button。我正在使用分組的Item頁面。 這些按鈕是基於返回的數據集生成的。 10條記錄將顯示10個按鈕。
<GridView.ItemTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Left" Width="Auto" Height="Auto" >
<Button Click="ItemView_ItemClick">
<StackPanel Margin="5" >
<TextBlock Tag="cntCustName" Style="{ThemeResource CntNormalTextBlockStyle}" Text="{Binding CUSTOMERNAME }"/>
<TextBlock Tag="cntCatCode" Style="{ThemeResource CntLrgTextBlockStyle}" Text="{Binding CATEGORYCODE}"/>
<TextBlock Tag="cntDay" Style="{ThemeResource CntNormalTextBlockStyle}" Text="{Binding DAY}"/>
</StackPanel>
</Button>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
我正在foreach循環中獲取數據。 我希望能夠根據我擁有的一些標准動態分配按鈕的樣式。
foreach (ContactData ContactData in _openContatcs)
{
if (ContactData.CFLAG)
{
//this is an example
Application.Current.Resources["contactSquare"] = ampStyle;
}
group.Items.Add(ContactData);
}
Styles are defined like this in a folder: Assests/Resources/BaseAPStyles.xaml
<Style x:Name="contactSquare" TargetType="Button">
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Height" Value="160"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="Margin" Value="5"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="Width" Value="160"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid Background="#ffffc600">
<ContentPresenter>
</ContentPresenter>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我的按鈕沒有顯示樣式。 我該如何實現?
使用x:key="contactSqaure"
在應用程序資源中查找樣式。
然后,訪問要設置樣式的按鈕,然后將樣式分配給按鈕。
yourButton.Style = Application.Resources.Current["contactSqaure"] as Style;
您還可以在C#中定義樣式,或編輯按鈕的依賴項屬性。
yourButton.Height = 160;
yourButton.VerticalAlignment = VerticalAlignment.Center;
您可以在加載時訪問數據模板中的按鈕。
<DataTemplate>
<Button Loaded="Button_Loaded" .../>
....
void Button_Loaded(object sender, RoutedEventArgs args)
{
(sender as Button).Style = yourStyle;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.