WPF Height/Width - wpf

In a custom WPF control I would like to set the width of the control to be a function of the Height. For example: Width = Height / 3 * x;
What would be the best way to accomplish this so that the control resizes (and initially sizes) correctly and fluidly?

You could bind Width to ActualHeight, and use converter to apply custom function. E.g. the following code makes Button always squared:
<Button Width="{Binding ActualHeight, RelativeSource={RelativeSource Self}}"/>
Hope this helps,
Cheers, Anvaka.

Related

Binding Canvas.Size to parent UserControl and then setting Canvas.Size in code

I have a wpf Canvas in a grid in a UserControl named 'root' and I bind the Width and Height of the canvas as follows:
Width="{Binding ElementName=root, Path=ActualWidth}"
Height="{Binding ElementName=root, Path=ActualHeight}"
On the canvas all kinds of DrawingVisual are drawn.
In code behind I set the Width end Height of my Canvas equal to ContentBounds.Right and ContentBounds.Bottom so that every DrawingVisual that I add to the canvas will be visible.
This seems to work all right but I am confused about the binding mentioned.
This is a one-way binding from the usercontrol's actual size to the size of the canvas.
Does the setting of Width and Height in code behind overrule this binding?
When I remove the binding the canvas is displayed equally as well, but the control is also used in other places and situations and might be needed then.
Does the setting of Width and Height in code behind overrule this binding?
Yes. Programmatically setting the value of a target property that has a one-way binding applied to it will clear the binding.

WPF: Expression with joined DataBinding and value

In the customized Slider template I've got, the main border's Height is bound to the TemplateBinding Height property, I want the Thumb element's height value to be higher by 2-3 pixels relatively to the TemplateBinding Height property I stated before.
Is there any elegant way to achieve that without getting ValueConverters and AttachedProperties involved?
Something like
Height="{TemplateBinding Height} + 3"
2 Options:
1) Consider setting the margin of your control whose Height is bound to -3. I think this will satisfy your description of "higher by 2-3 pixels". If not -3, you can play with other margin values.
2) If 1 doesn't work, then you'll need to write an IValueConverter class and set it as the converter for your binding. Unfortunately, TemplateBinding doesn't support converters since they are meant to be lightweight connections to your Control's forward-facing properties.
Your binding will instead be:
Height="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Height, Converter={StaticResource ReferenceToMyPlus3Converter}}"
Here's a link to an example of writing a converter.

ComboBox with Arrow / Symbol only

I'd like to have a combobox that is really small in width and displays only the arrow. Background is to save space beside other buttons in the layout, and not visualize the selected item. But the dropdown should not be limited in width. I didnt find any properties to hide the inner textbox. Is it possible ?
In your ContentPresenter, you could set the Visibility depending on the ActualWidth of the ComboBox:
<ContentPresenter Visibility={Binding RelativeSource={RelativeSource TemplatedParent}, Path=ActualWidth, Converter={StaticResource ResourceKey=WidthToVisibility}}
In your Converter, you check if the value is 8 or less, then return Visibility.Collapsed, else return Visibility.Visible.

How to get or compute actual width of ListBox without Vertical Scrollbar if visible

I wonder how to get or compute actual width of ListBox without Vertical Scrollbar if visible.
What I want to do is changing width of each items inside ListBox without being covered by Vertical Scrollbar.
Width="{Binding ActualWidth,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}
At least, above binding tell me the actual width of ListBox, but this one does not handle width of vertical scrollbar.
Is there any good way to solve this?
Try binding to the ViewportWidth property of the containing ScrollViewer instead.
Like this:
Width="{Binding Path=ViewportWidth, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ScrollViewer}}"
Maybe the default will help you: just don't set Width at all!
At least in my case it was totally sufficient to not set the Width property. While Mårten Wikström's answer is completely correct I found out my actual problem was I had specified (unnecessarily) Width and did this in a wrong way (the same way Aki24x has reported).
This is a simplified case of my example. The solution was to just delete line 4.
1 <ListBox.ItemTemplate>
2 <DataTemplate>
3 <Border
4 Width="..."
5 >

WPF window size

I am creating a Notepad like application in WPF. I want to set the window form height and width according to screen size . How can i get Screen height and width ?
Just bind SystemParameters properties to your window properties.
<Window x:Class="YourWindow"
Height="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Height}"
Width="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Width}">
See System.Windows.SystemParameters
You have properties like
PrimaryScreenWidth
PrimaryScreenHeight
VirtualScreenHeight
VirtualScreenWidth
WorkArea
etc.
This question might help as well: How can I get the active screen dimensions?

Resources