The member "Height" is not recognized or is not accessible - c#

I want to set the Height property in a setter of a style but it shows the error at design time. It will work without problem at runtine.
The type inherits from control, which inherits for framework element, that has a Height property.
Can someone explain why and how I can fix it or get rid of the message?
<Style TargetType="{x:Type materialDesign:PackIcon}"
BasedOn="{StaticResource {x:Type materialDesign:PackIcon}}">
<Setter Property="Height"
Value="30" />
</Style>
I think it has nothing to do with the class itself, but here is the code anyway:
Apparently it has to do with the class itself:
materialDesign:PackIcon
Edit1:
After prefixing with FrameworkElement I get the error messagages:
PackIconExtension' type must derive from FrameworkElement or FrameworkContentElement.
and
The resource "materialDesign:PackIcon" could not be resolved.
Seemingly it is an extension - but this surpasses my wpf knowledge.
<Setter Property="FrameworkElement.Height"
Value="30" />
Edit2:
Here is the code of the extension: materialDesign:PackIconExtension

Related

WPF Override Style Value

I'm using MaterialDesignInXaml for WPF which provides 3rd party controls and styles. I need to edit one of these styles by changing one property.
I am using an Expander control which has a template creating a bunch of child controls. I've discovered the child 'Border' control (4 layers deep) has the property (padding) which I need to set to zero.
See this output from Snoop showing the property I need to change:
Link to image
My question is how can I do this? I've tried extending the style used by the control as follows, but it isn't changing anything so I assume I'm doing something wrong?
<Style TargetType="{x:Type Expander}"
x:Key="MaterialDesignExpanderHeadless"
BasedOn="{StaticResource MaterialDesignExpander}">
<Style.Resources>
<Style TargetType="{x:Type Border}">
<Setter Property="Padding" Value="0"></Setter>
</Style>
</Style.Resources>
</Style>
I am able to use the style like this. And I know this is working for sure:
<Expander Header="Header Content" Style="{StaticResource MaterialDesignExpanderHeadless}">
Some Content
</Expander>
You're right, this method should work. Something else is setting the border's padding.
Snoop is telling you the padding is defined by the parent template, which could be the HeaderSite (ToggleButton).
You could try to extend the ToggleButton style (BasedOn) or redefine it locally.

Setter not working for margin on user control in WPF

I have created a UserControl that is working just fine. If I set the margin in code all goes well:
<cbtn:BillLister Margin="10,5,10,5" />
However, when i try to set it for everyone of those that are in the wrappannel they are in, this doesn't work:
<WrapPanel.Resources>
<Style TargetType="cbtn:BillLister">
<Style.Setters>
<Setter Property="Margin" Value="150,35,50,35" />
<Setter Property="Background" Value="Black"/>
</Style.Setters>
</Style>
The Background property is working however, so i know that it is targeting the correct UserControls.
I did find this: How to give my custom control an overridable default margin? I tried what he did in there, and still no change.
public partial class BillLister : UserControl
{
public BillLister()
{
InitializeComponent();
Labelcount.Content = "0";
}
static BillLister()
{
MarginProperty.OverrideMetadata(typeof(BillLister), new FrameworkPropertyMetadata(new Thickness(30, 130, 30, 30)));
}
...
Does anyone have any idea what might be going on here? I would hate to have to set the margin on each one manually.
Update to clarify: When trying the second version i don't use the inline margin from the first one. Also, the margin fromthe second example is not applied at all.

Using a converter for more than one binding?

I would like to do something like this:
<Style TargetType="{x:Type Binding}">
<Setter Property="Converter" Value="{StaticResource converter1}"/>
</Style>
That doesn't work though. So how do I tell more than one binding which converter to use without writing it explicitly for every single one?
I'm sorry to say, but there are actually two reasons you cannot do this.
<Style TargetType="{x:Type Binding}">
<Setter Property="Converter" Value="{StaticResource converter1}"/>
</Style>
Firstly, you cannot create a style for System.Windows.Data.Binding because it does not meet the requirements for styling. The TargetType must derive from either FrameworkElement or FrameworkContentElement. Alas, Binding inherits from BindingBase, then MarkupExtension, then Object and so it cannot be styled.
Secondly, Setter.Property is of type DependencyProperty. Binding.Converter is not a dependency property, so it simply cannot have a value bound to it.
So, you will have to repeat the Converer={StaticResource converter1} within the braces of each XAML {Binding} markup extension.

Unknown attachable member 'Grid.Row'and 'Grid.Column'

I'm a long time WPF designer yet new to windows app development. I'm trying to bind a collection of objects onto a grid yet keep getting the error Unknown attachable member '(Grid.Row)' on element 'FrameworkElement' and Unknown attachable member '(Grid.Column)' on element 'FrameworkElement'.
Can someone please explain to me the how to set the various Grid attached properties via style?
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid>
<!-- Column and row definitions omitted for brevity -->
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="FrameworkElement">
<Setter Property="(Grid.Row)" Value="{Binding Row}" />
<Setter Property="(Grid.Column)" Value="{Binding Column}" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
Don't use a PropertyPath. All you need is a qualified Owner.Property string.
<Setter Property="Grid.Row" Value="{Binding Row}" />
Taken from PropertyPath XAML Syntax
Some style and template properties such as Setter.Property take a
qualified property name that superficially resembles a PropertyPath.
But this is not a true PropertyPath; instead it is a qualified
owner.property string format usage that is enabled by the WPF XAML
processor in combination with the type converter for
DependencyProperty.
It turns out that there are actually 3 problems with the code I posted above.
As #LPL correctly identified Setter.Value takes a qualified property name where a PropertyPath was being used. The fix here is to drop the parentheses: <Setter Property="Grid.Row" ... /> and <Setter Property="Grid.Column" ... />.
The second issue is with the style target type. It turns out that the metro Grid attached properties can't be applied to FrameworkElement's. The solution here is to update the target type with something more specific: <Style TargetType="ContentPresenter" />.
Finally as with Silverlight, the value property of metro setters don't support bindings. Consequently even after fixing the previous two errors, the setter is actually trying to set the grid attached properties to an instance of type Binding. While not as straight forward, all the details of a solution may be found here. In summary you can use the setter to set a custom attached property, which will in turn set up any desired binding.

What is the recommended way to skin an entire application in WPF?

I want my WPF application to be skinnable, by applying a certain XAML template, and the changes to be application wide, even for dynamic controls or controls that aren't even in the visual/logical tree.
What can I use to accomplish this type of functionality? Are there any good resources or tutorials that show how this specific task can be done?
The basic approach to take is using resources all through your application and dynamically replacing the resources at runtime.
See http://www.nablasoft.com/alkampfer/index.php/2008/05/22/simple-skinnable-and-theme-management-in-wpf-user-interface/ for the basic approach
The replacing of resource will work but I found "structural skinning" to be more powerfull! Read more about it on CodeProject...
http://www.codeproject.com/KB/WPF/podder1.aspx
I have found the way to apply generic templates to all controls without using template keys. The solution is to use the type of the control as the Style key.
Example:
<Application.Resources>
<Style x:Key="{x:Type Button}" TargetType="{x:Type Button}">
<Setter Property="Button.Background" Value="CornflowerBlue"/>
<Setter Property="Button.Template">
<Setter.Value>
<ControlTemplate x:Name="MyTemplate">
...
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Application.Resources>
here the Style key is x:Key="{x:Type Button}", so the style will be applied to all controls of type button without the control declaring the Style property to be a static or dynamic resource.

Categories

Resources