我从我的App.xaml中引用了ExpressionDark.xaml,它工作得很好,但是当我尝试在ItemsControl中使用ItemContainerStyle时,ItemsControl中的项恢复为基本样式。
<ItemsControl Grid.Column="1" VerticalAlignment="Center" Margin="10">
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Margin" Value="5" />
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.Items>
<TextBlock Text="{Binding Error}" />
<TextBox Text="{Binding Path=Username,UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True}" Validation.ErrorTemplate="{StaticResource validationTemplate}"></TextBox>
<TextBox Text="{Binding Path=Password,UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True}" Validation.ErrorTemplate="{StaticResource validationTemplate}"></TextBox>
<Button VerticalAlignment="Center" HorizontalAlignment="Right" Command="{Binding SignInCommand}" IsEnabled="{Binding CanSignIn}" Content="Sign In"></Button>
<TextBox Text="{Binding Path=Username}"></TextBox>
</ItemsControl.Items>
</ItemsControl>我只是想为垂直样式找到一个好的控件(轻松地在项目之间添加边距),所以也许有一种更好的方法可以不覆盖App.xaml中指定的样式。
提亚
发布于 2014-06-20 15:56:27
如果您指定一个“就地”样式,它将被视为一个全新的样式。因此,对于该元素,来自ExpressionDark.xaml的默认样式是forgotton。
要避免这种情况,您必须做的是:使用BasedOn=引用基本样式
<ItemsControl.ItemContainerStyle>
<Style BasedOn="{StaticResource Existing}">
<Setter Property="Margin" Value="5" />
</Style>
</ItemsControl.ItemContainerStyle>查找控件的相应默认样式。并用ExpressionDark.xaml.中的资源密钥替换Existing您可以标识它,因为它将具有适当的TargetType属性集:
<Style TargetType="{x:Type ListBoxItem}"> x:Key=...其中ListBoxItem是您正在使用的控件(要重新设置样式)
您可以考虑使用ListBox而不是ItemsControl,因为它将ListBoxItem作为容器。
发布于 2011-01-06 07:14:51
他们使用边距的方式很好,但是当你使用与App.xaml中的样式不同的样式时,它肯定不会使用App.xaml中的样式。
这就是它在WPF中的工作方式,控件使用“最接近”的样式,而且由于您是直接将此样式写入控件中,因此它也使用该样式。
您可以使用基于ExpressionDark.xaml的'BaseOn‘属性在app.xaml中创建新样式,但您需要添加:
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Margin" Value="5" />
</Style>
</ItemsControl.ItemContainerStyle>https://stackoverflow.com/questions/4609932
复制相似问题