任何人都可以解释OverridesDefaultStyle实际对控件做了什么.我读过MSDN,但我不明白.
我在app.xaml中有一个DataTemplate,它将视图绑定到viewmodel.
<Application.Resources>
<DataTemplate DataType="{x:Type vm:someviewmodeltype}">
<vw:somevwcontrol />
</DataTemplate>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
如果没有样式,则不会应用上述模板.我放风格的那一刻,像...
<Application.Resources>
<DataTemplate DataType="{x:Type vm:someviewmodeltype}">
<vw:somevwcontrol />
</DataTemplate>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="20"></Setter>
</Style>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
datatemplate被拿起并产生预期的结果......我不确定那里发生了什么......有人可以澄清一下吗?
谢谢.
在即使是最小的WPF例子和原型我一直在做,在<Windows.Resources>开始臃肿快.把它重新放入app.xaml我的Windows和UserControls,但它很难组织(Visual Studio"XAML折叠"功能没有任何帮助,因为你只有一个页面充满了"Style ..."这个词).
另外,我正在努力想出一种易于记忆和有组织的方式来命名我的风格.最好的方法我发现它只是为了冗长和描述性,所以我得到这样的东西:BottomMainLeftScrollViewerStyle等.但这有其局限性,很快也会让人感到困惑.我决定使用camelCase作为样式名称,以便在XAML的页面和页面中轻松发现它们.
您有什么策略可以防止WPF资源变得笨拙?
<Window.Resources>
<local:CutOffConverter x:Key="AgeConverter" Cutoff="30"/>
<Style TargetType="Grid" x:Key="customerGridMainStyle">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint=".5,.5">
<GradientStop Offset="0.0" Color="#888"/>
<GradientStop Offset="1.0" Color="#ccc"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="StackPanel" x:Key="mainStackPanelStyle">
<Setter Property="HorizontalAlignment" Value="Left"/>
</Style>
<Style TargetType="ScrollViewer" x:Key="mainScrollViewerStyle">
<Setter Property="Height" Value="250"/>
</Style>
<Style TargetType="ListBox" x:Key="mainListBoxStyle">
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Margin" Value="10"/>
</Style>
<ObjectDataProvider x:Key="customers"
ObjectType="{x:Type local:Customer}"
MethodName="GetAllCustomers"/>
<DataTemplate DataType="{x:Type local:Customer}">
<Grid x:Name="MainGrid" Style="{StaticResource customerGridMainStyle}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/> …Run Code Online (Sandbox Code Playgroud) 我刚刚注意到,当我ViewModel从后台工作线程更改我的(MVVM)中的绑定属性时,我没有得到任何异常,并且视图已正确更新.这是否意味着我可以安全地依赖于wpf数据绑定编组ViewModelUI线程中的所有更改?我想我已经读过一个应该确保(在ViewModel)INotifyPropertyChanged.PropertyChanged线程上触发的内容.这有什么改变在3.5或什么?
我正在尝试这样做:
<TextBlock Text="{Binding Path=Text,
Converter={StaticResource stringFormatConverter},
ConverterParameter='\"{0}\"'}" />
Run Code Online (Sandbox Code Playgroud)
但这显然不是将引用引入XAML绑定字符串的方法.
让"\"{0}"\"在这里工作的适当方法是什么?
我有一个StatusBar我想把一个TextBlock停靠在左边并ProgressBar填充剩余的宽度.在我使用WPF的所有其他经验中ProgressBar,它将填充其给定的容器,但是在这种情况下它根本不会自动调整大小,它只是显示为一个小圆圈.手动设置它的高度和宽度工作正常,但我希望它可以缩放到剩余的宽度StatusBar.
这是我的XAML StatusBar:
<StatusBar DockPanel.Dock="Bottom" Height="25">
<StatusBarItem DockPanel.Dock="Left">
<TextBlock x:Name="lblStatus" Margin="5,0,0,0"/>
</StatusBarItem>
<StatusBarItem>
<ProgressBar x:Name="pgbStatus" />
</StatusBarItem>
</StatusBar>
Run Code Online (Sandbox Code Playgroud) 好的,这是我的XAML:
<TextBlock Text="{Binding Path=InstanceName}"></TextBlock>
Run Code Online (Sandbox Code Playgroud)
如果InstanceName是null或空字符串,我想要Visibility="Collapsed".否则我想要Visibility="Visible".我该怎么办?
我觉得这很容易,但我猜不是.
我有两页加载到我的帧控件中.我希望能够从一个页面到下一个页面具有漂亮的幻灯片效果,或者只是一个简单的淡入效果.
似乎无法在互联网上的任何地方找到它.
更新1 接受的答案很好,但我在这里找到了更好的答案. http://www.japf.fr/2008/07/8/comment-page-1/
更新2
如果您相信它我找到了更好的解决方案.
http://fluidkit.codeplex.com/
所以有人建议使用WPF TreeView,我想:"是的,这似乎是正确的方法." 现在,几个小时后,我简直无法相信使用这个控件有多困难.通过一系列研究,我能够使TreeView`控件正常工作,但我找不到"正确"的方法来将所选项目添加到视图模型中.我不需要从代码中设置所选项目; 我只需要我的视图模型就可以知道用户选择了哪个项目.
到目前为止,我有这个XAML,它本身不是很直观.这都在UserControl.Resources标记内:
<CollectionViewSource x:Key="cvs" Source="{Binding ApplicationServers}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="DeploymentEnvironment"/>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
<!-- Our leaf nodes (server names) -->
<DataTemplate x:Key="serverTemplate">
<TextBlock Text="{Binding Path=Name}"/>
</DataTemplate>
<!-- Note: The Items path refers to the items in the CollectionViewSource group (our servers).
The Name path refers to the group name. -->
<HierarchicalDataTemplate x:Key="categoryTemplate"
ItemsSource="{Binding Path=Items}"
ItemTemplate="{StaticResource serverTemplate}">
<TextBlock Text="{Binding Path=Name}" FontWeight="Bold"/>
</HierarchicalDataTemplate>
Run Code Online (Sandbox Code Playgroud)
这是树视图:
<TreeView DockPanel.Dock="Bottom" ItemsSource="{Binding Source={StaticResource cvs}, Path=Groups}"
ItemTemplate="{StaticResource categoryTemplate}">
<Style TargetType="TreeViewItem">
<Setter Property="IsSelected" Value="{Binding Path=IsSelected}"/>
</Style>
</TreeView> …Run Code Online (Sandbox Code Playgroud) wpf ×10
xaml ×4
mvvm ×2
styles ×2
app.xaml ×1
binding ×1
c# ×1
data-binding ×1
datatemplate ×1
frame ×1
progress-bar ×1
quotes ×1
resources ×1
selecteditem ×1
statusbar ×1
textblock ×1
treeview ×1
visibility ×1