<UserControl x:Class="FlowItem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="300" Height="300">
<UserControl.Resources>
<Style TargetType="Label">
<Style.Setters>
<Setter Property="Background" Value="AliceBlue" ></Setter>
</Style.Setters>
</Style>
</UserControl.Resources>
<DockPanel Height="300" Width="300" LastChildFill="False" Background="Transparent">
<Label Height="28" DockPanel.Dock="Top"></Label>
<Label Height="28" DockPanel.Dock="Bottom">What ever</Label>
<Label Width="28" DockPanel.Dock="Left"></Label>
<Label Width="28" DockPanel.Dock="Right"><开发者_开发技巧/Label>
</DockPanel>
How could I set the Labels' Backgrounds via Styles? Thanks!
The actual error tells me that Setter.Seal() is being called on the setter and later items are being added or removed from that collection (ie. Add, Insert, Remove, or Clear was called). I can't see anything in your XAML that would cause that, and the code compiles fine for me.
I created an empty VS2008 C# "WPF Application" project, add your XAML to it as a loose XAML file with no code behind, and added the missing tag at the end. The resulting project compiles fine, and I can add the UserControl to the window and see it.
Try using the same procedure. If it works in an empty project but not in yours, compare the projects to find what is causing your problem. Here ar some things to check:
- Do the projects have the same references?
- Do the projects have the same build settings, including target framework?
- Does your regular project use the terms "UserControl", "Style", "Setter", etc, anywhere?
- Does your regular project if you gut the code-behind?
If your XAML doesn't even work in a brand-new empty project created from the C# WPF Application tempate, here are the things to check:
- Whether you have beta software (especially VS.NET or NET Framework) installed, or if you have ever had it, and if so is it entirely removed
- Try compiling your project with MSBuild to see if the problem might be related to the IDE
- Try downloading a working WPF project that uses styles and compiling it, for example Kevin's Bag Of Tricks (sp?)
- As a last resort try reinstall NET Framework, and if that doesn't work, Visual Studio
The code you posted is in fact setting the label's background via styles. It works well.
I assume you are trying to do something like this and are being told the SetterCollectionBase is sealed:
setter.Value = Brushes.Green;
To change the background brush set by the style in code, you can define a DependencyProperty somewhere, and bind to it. For example if your FlowItem class has a DependencyProperty "LabelBackgroundColor" you could set the binding in the style like this:
<Setter Property="Background" Value="{Binding LabelBackgoundColor, RelativeSource={RelativeSource FindAncestor, AncestorType=local:FlowItem}}" />
Note that you will also need a xmlns:local= definition.
With the DependencyProperty declared and the new setter value, you can do this in code:
myFlowItem.LabelBackgroundColor = Color.Green;
In general I recommend against creating properties like LabelBackgroundColor directly on UserControls. Usually a better solution is to use trigger based on an actual property of FlowItem that reflects the purpose, for example you might use an "IsModified" property if you want the label color to change if the FlowItem is modified.
A have similar error: "After a Style is in use (sealed), it cannot be modified."
and for me problem was solved, when I REBUILD whole solution.
Probably VS (I use 2012) something wrong linked in the source or references
精彩评论