我有一个非常棘手的问题:
我正在使用ListView控件,其ItemsSource设置为CollectionViewSource,包括PropertyGroupDescription以对ListView元素进行分组.CollectionViewSource看起来像这样:
<CollectionViewSource x:Key="ListViewObjects">
<CollectionViewSource.Source>
<Binding Path="CurrentListViewData"/>
</CollectionViewSource.Source>
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="ObjectType" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
Run Code Online (Sandbox Code Playgroud)
在ListView中,我使用自定义组头,如下所示:
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Margin" Value="5"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True">
<Expander.Header>
<DockPanel>
<TextBlock Text="{Binding Path=Items[0].ObjectType />
</DockPanel>
</Expander.Header>
<Expander.Content>
<ItemsPresenter />
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListView.GroupStyle>
Run Code Online (Sandbox Code Playgroud)
如您所见,Expander的IsExpanded属性设置为true.这意味着每当刷新ListView时,都会扩展所有Expander控件.
但是我想保存每个Expander的最后状态.我无法找到一种方法来保存每个ObjectType的Expander状态列表.我正在尝试使用绑定的HashTable和Converter,但是我没有将ObjectType作为ConverterParameter提供,因为它总是作为字符串传递.但这可能不是解决方案.
有人可以给我一个解决方案的提示或想法吗?:)
我将ListView放在View的中间行.该视图包含在SizeToContent设置为WidthAndHeight的窗口中.ListView最初为空,但底层的ViewModel在此过程中填充此列表视图.
中间Grid.Row高度设置为*以填充窗口的可用大小.当ListView接收新项目时,它将在某些时候扩展窗口大小,而不是在ListView中显示ScrollViewer.如何防止此行为将SizeToContent设置为WidthAndHeight,Grid.Row高度为*但不具有ListView扩展窗口尺寸?
这是窗口的代码(Workspace属性将包含ViewModel):
<Window x:Class="Views.ContainerWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="{Binding Title}"
SizeToContent="WidthAndHeight">
<ContentControl Content="{Binding Workspace}"/>
</Window>
Run Code Online (Sandbox Code Playgroud)
提供的ViewModel的视图如下所示:
<UserControl x:Class="Views.SomeView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
MinHeight="450">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0"
TextWrapping="Wrap"
Margin="5"
Text="Some description text"/>
<ListView Grid.Row="1"
ItemsSource="{Binding ItemsList}"
Margin="5">
<ListView.View>
<GridView>
...
</GridView>
</ListView.View>
</ListView>
<Button Grid.Row="2"
HorizontalAlignment="Right"
Command" Value="{Binding CloseCommand}"/>
</Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud) 我的问题出现在.NET 3.5 SP1中的WPF,可以描述如下:
我有一个默认Style命中TextBlock我的UI中的所有元素.这就是它的样子:
<Style TargetType="{x:Type TextBlock}">
<Setter Property="TextTrimming" Value="CharacterEllipsis"/>
<Setter Property="Foreground" Value="Red"/>
</Style>
Run Code Online (Sandbox Code Playgroud)
这适用于所有人TextBlock.除此之外,我有一个Button样式,包括ControlTemplate看起来像这样(缩短):
<Style x:Key="MyButtonStyle" TargetType="{x:Type Button}" BasedOn="{x:Null}">
<Setter Property="Foreground" Value="Green"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="Border"
Background="{TemplateBinding Background}"
BorderThickness="{TemplateBinding BorderThickness}"
Padding="{TemplateBinding Padding}"
Height="24"
BorderBrush="{TemplateBinding BorderBrush}">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
TextBlock.Foreground="{TemplateBinding Foreground}"/>
</Border>
<ControlTemplate.Triggers>...</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
请注意行TextBlock.Foreground="{TemplateBinding Foreground}"中ContentPresenter.这应该将按钮文本设置为绿色,实际上它在Visual Studio的设计器视图中.但是当我编译并运行程序时,按钮文本为红色,文本颜色由默认TextBlock样式设置.我用Snoop验证了这一点.
如何防止默认TextBlock样式覆盖该TextBlock.Foreground值?在这种情况下,OverridesDefaultStyle财产ContentPresenter …
我试图在我的WPF应用程序中做一个相当简单的事情,或者至少我认为它很简单.
我有一个窗口,在后面的代码中包含一个int属性.我们称之为IntProperty.Window实现了INotifyPropertyChanged接口,IntProperty在更改时触发通知.它看起来像这样:
public int IntProperty
{
get
{
return intProperty;
}
private set
{
if (value != intProperty)
{
intProperty = value;
NotifyPropertyChanged("IntProperty");
}
}
}
Run Code Online (Sandbox Code Playgroud)
在xaml文件中,我定义了一个在Fill属性中设置了特定颜色的Rectangle.
<Rectangle x:Name="MyRectangle" Fill="Red" Width="100" Height="5" Margin="3,0,0,0" VerticalAlignment="Center" />
Run Code Online (Sandbox Code Playgroud)
我现在要做的是根据IntProperty的值设置Fill属性.我正在寻找一些简单的xaml线来触发这种变化.
它应该看起来像这样(AppWindow是Window的名称):
<Rectangle x:Name="MyRectangle" Fill="Red" Width="100" Height="5" Margin="3,0,0,0" VerticalAlignment="Center">
<Rectangle.Triggers>
<Trigger SourceName="AppWindow" Property="IntProperty" Value="1">
<Setter TargetName="MyRectangle" Property="Fill" Value="Green" />
</Trigger>
</Rectangle.Triggers>
Run Code Online (Sandbox Code Playgroud)
有没有简单的方法来做到这一点?到目前为止,我找不到任何简单的解决方案.如果有人能指出我正确的方向,我将不胜感激.
谢谢!