Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用Blend向列表框项目添加样式_C#_Wpf_Windows Phone 8_Windows 8_Expression Blend - Fatal编程技术网

C# 如何使用Blend向列表框项目添加样式

C# 如何使用Blend向列表框项目添加样式,c#,wpf,windows-phone-8,windows-8,expression-blend,C#,Wpf,Windows Phone 8,Windows 8,Expression Blend,我在XAML中有一个列表框代码,下面是我的代码: <!--ContentPanel - place additional content here--> <Grid x:Name="ContentPanel" Grid.Row="2" Margin="12,0,12,0"> <ListBox Name="listBox" HorizontalContentAlignment="Str

我在XAML中有一个列表框代码,下面是我的代码:

<!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="2" Margin="12,0,12,0">
            <ListBox Name="listBox" 
                     HorizontalContentAlignment="Stretch" 
                     VerticalContentAlignment="Stretch" 
                     SelectionChanged="TopicListboxSelectionChanged"
                     ScrollViewer.VerticalScrollBarVisibility="Disabled" >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Margin="0,0,0,10" Orientation="Horizontal">
                                <Border Background="Gray">
                                    <TextBlock Height="50" 
                                       HorizontalAlignment="Center"
                                       VerticalAlignment="Center"
                                       Text="{Binding Path=Value}"></TextBlock>
                                </Border>
                            </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>              
        </Grid>

现在我需要向列表框项添加样式,如下图所示,以及onclick效果

如何使用Blend?因此,如果明天我需要为任何其他项目添加样式,我可以自己添加,而无需寻求任何帮助


如果有任何教程可供学习,请提供相关链接。

在页面中声明样式

下面是页面中的所有命名空间声明

做个标签就行了

<phone:PhoneApplicationPage.Resources>
</phone:PhoneApplicationPage.Resources>   

并在其中声明以下样式

        <Style x:Key="style_ColorButton" TargetType="Button">
        <Setter Property="Background" Value="Gray"/>
        <Setter Property="BorderBrush" Value="Gray"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/>
        <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilySemiBold}"/>
        <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMediumLarge}"/>
        <Setter Property="Padding" Value="10,3,10,5"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid Background="Transparent">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver"/>
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="White"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="Black">

                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="Gray"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="0">
                            <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

将此样式与按钮一起使用

<Button Height="40" Width="40" Name="btnAcceptCrop" Click="btnAcceptCrop_Click" Style="{StaticResource style_ColorButton}"/>


onclick需要什么样的效果?@AmanKhandelwal首先我需要学习如何使用blend应用atyle,当单击时,它应该用用户选择的任何颜色指示用户。您需要了解任何控件的视觉状态(按下、释放、聚焦等),您在datatemplate中使用的父级是stackpanel,而stack panel没有这种可视状态。我建议您改为使用按钮。@AmanKhandelwal ok您是说StackPanel在那里添加按钮?否,从datatemplate中删除所有内容,添加一个按钮,并将其内容绑定到content=“{Binding Path=Value}”。这将产生多个按钮,并显示onclick效果,以及样式。。我会将其更新为答案。好的,我会检查一次,但你是如何做到的?我需要学习如何简单,只是一个简单的链接来开始如何添加列表框ItemContainerStyle?当你不想在列表框中定义数据模板时,请检查此链接,以便使用按钮是更好的方法?