border.background image fixed - wpf

I'm starting in WPF and I'm developing an application that has a background (.png) applied as follow:
'<Border.Background>'
'<ImageBrush ImageSource="final.png" AlignmentY="Top"/>'
'</Border.Background>'
No problems until here. My problem is because I have an Expander in my window and, in my collapsed method I make an alteration in window size, as shown:
private void Expander_Collapsed(object sender, RoutedEventArgs e)
{
windowFrame.Height = 400;
}
I would like that image, used as background, stay fixed after expander action, ie, when the window is expanded the background is shown full, and when is Collapsed the background shall be cutted.
In analogy to c#, is similar like BackgroundImageLayout = none. You can redim your WindowsForm and the image will be showing according to windows size.
I need finish this job and I hope find my answer here.
Thanks a lot!

<ImageBrush ImageSource="final.png" AlignmentY="Top" Stretch="None"/>
The default value for Stretch is "Fill"

Related

windows forms application - how to make the tabs in a tab control get full width?

So i'am working with tabControl in windows forms application and i want to make the tabs get full width regardless whether the application window is maximized or not.
When the window isn't maximized everything appears great:
But when the window gets maximized the tabs doesn't get the full width:
Is there any known way to fix this problem?
Thanks in advance
You can achieve this in some way by modifying the ItemSize property as described bellow, else you'd have to draw the tab page selectors yourself.
public Form1()
{
InitializeComponent();
tabControl1.SizeMode = TabSizeMode.Fixed;
tabControl1.ItemSize = new Size((tabControl1.Width / tabControl1.TabPages.Count) - 1, tabControl1.ItemSize.Height);
}
//Hook to form or parent container Resize event, either Resize or ResizeEnd.
private void Form1_ResizeEnd(object sender, EventArgs e)
{
tabControl1.ItemSize = new Size((tabControl1.Width / tabControl1.TabPages.Count) - 1, tabControl1.ItemSize.Height);
}

How can I limit the height of a window to a percent of the screen resolution using the MVVM pattern?

I have a window that is configured in this way:
<Window x:Class="Catalogo.Views.dlgGenerosContenidosAsignarView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="dlgGenerosAsignar"
SizeToContent="WidthAndHeight"
WindowStartupLocation="CenterOwner">
I want the window to adjust to its content, so I use SizeToContent="WidthAndHeight". But if the content is too big, it can outsize the screen, so I would like to limit the value of the MaxHeight attribute to the height of the screen.
How could I do this using the MVVM pattern?
I could use binding to a property in my ViewModel that uses the System.Windows.SystemParameters to get the height of the screen and set a property that binds to the View, but I think that the size of the window should be set in the View, not the ViewModel, so I would like to know if there is another solution.
There is a lot of confusion around using the code behind, when following the MVVM methodology. This is really a perfect situation where you would want to use the code behind. As you correctly noticed, this is purely view related and should not be in the view model. It has no reason, or possible benefit from being in there.
So, if I were you, I'd add a handler to the Window.Loaded event, get your measurements and set the Window.Height to the desired Height:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// Assuming that you want the Height to be 80% of the screen Height
Height = SystemParameters.PrimaryScreenHeight * 0.8;
}

WPF ContextMenu placement adjusted event

Does anyone know of how I can determine when the ContextMenu get its placement automatically adjusted due to being too close to the edge of the screen?
My scenario is that I have a ContextMenu that has 2 rounded corners and 2 square corners. When the menu opens down I round the bottom 2, and if the menu is opening upwards then I round the top 2. The problem is that I haven't found an event or property to bind to that tells me when the menu gets its direction automatically changed.
Here's some simplified sample code to try out. If you click when the window is at top of screen then menu goes down. If you move window to bottom of screen then the menu will go up.
<Window x:Class="menuRedirection.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="100" Width="200">
<DockPanel Name="panel" ContextMenuOpening="DockPanel_ContextMenuOpening">
<DockPanel.ContextMenu>
<ContextMenu>
<MenuItem Header="item"/>
<MenuItem Header="item"/>
<MenuItem Header="item"/>
<MenuItem Header="item"/>
</ContextMenu>
</DockPanel.ContextMenu>
<Rectangle DockPanel.Dock="Bottom" Name="menuTarget" Fill="Red" Height="10"/>
<TextBlock DockPanel.Dock="Top" Text="right click for context menu"/>
</DockPanel>
</Window>
private void DockPanel_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
ContextMenuService.SetPlacement(panel, PlacementMode.Bottom);
ContextMenuService.SetPlacementTarget(panel, menuTarget);
}
Here's what the real application looks like so you can see my problem with needing to know to adjust my rounded corners.
As far as I can tell, this is not possible.
Using JustDecompile, I traced this functionality to the UpdatePosition method in the Popup class. The final location seems to be set here:
this._positionInfo.X = num4;
this._positionInfo.Y = num5;
this._secHelper.SetPopupPos(true, num4, num5, false, 0, 0);
_secHelper is a helper class of type PopupSecurityHelper, and seems to just be an internal helper...And, none of these result in an event or even a public property being changed.
Here is an MSDN article explaining how popup positioning is determined in general (The 'When the popup encounters the edge of a screen' describes your scenario).
However, this article explains how you can use the CustomPopupPlacementCallback to override these behaviors somewhat. However, this still uses a PopupPrimaryAxis, which should flip the menu as necessary, and will result in the same problem.
The only other thing I could think of is that you could look into the PlacementRectangle and maybe poll the size and location similar to how UpdatePosition does things...or just check the popup itself just like UpdatePosition does.
This is a private method, though. So, any logic you try to mimic could change in a future version of the framework.
UPDATE
Also, you could possibly try bastardizing PointToScreen or PointFromScreen, but that would be very convoluted code if it worked...
I was unable to find a true WPF solution but Justin's comment lead me down the path of experimenting with comparing the menu's location with the PlacementTarget's location.
First step was to subscribe to the contextMenu.Loaded event (this fires after layout has been processed but before it's fully visible on the screen).
<ContextMenu ContextMenu.Loaded="ContextMenu_Loaded">
And then when that fires I can figure out if the menu was internally switched to the alternate placement for my requested placementMode. If it was reversed then I go ahead and adjust my rounded corners accordingly.
NOTE: i initially had used getWindowRect and compared the menu Rect with the target's Rect, but found that the menu Rect was always returning the prior instance's location. To avoid this problem I now get the relevant screen's workingArea and manually see if the menu fits.
NOTE2: be sure your menu's template results in the same window height for both inverted and regular display. Otherwise, your calculation could be off since getWindowRect returns the last menu's size.
void ContextMenu_Loaded(object sender, RoutedEventArgs e)
{
bool reversed = isMenuDirectionReversed(this.ContextMenu);
//existing styles are read-only so we have to make a clone to change a property
if (reversed)
{//round the top corners if the menu is travelling upward
Style newStyle = new Style(typeof(ContextMenu), this.ContextMenu.Style);
newStyle.Setters.Add(new Setter { Property = Border.CornerRadiusProperty, Value = new CornerRadius(10, 10, 0, 0) });
this.ContextMenu.Style = newStyle;
}
else
{ //since we may have overwritten the style in a previous evaluation,
//we also need to set the downward corners again
Style newStyle = new Style(typeof(ContextMenu), this.ContextMenu.Style);
newStyle.Setters.Add(new Setter { Property = Border.CornerRadiusProperty, Value = new CornerRadius(0, 0, 10, 10) });
this.ContextMenu.Style = newStyle;
}
}
Evaluation method:
private bool isMenuDirectionReversed(ContextMenu menu)
{
//get the window handles for the popup' placement target
IntPtr targetHwnd = (HwndSource.FromVisual(menu.PlacementTarget) as HwndSource).Handle;
//get the relevant screen
winFormsScreen screen = winFormsScreen.FromHandle(targetHwnd);
//get the actual point on screen (workingarea not taken into account)
FrameworkElement targetCtrl = menu.PlacementTarget as FrameworkElement;
Point targetLoc = targetCtrl.PointToScreen(new Point(0, 0));
//compute the location for the bottom of the target control
double targetBottom = targetLoc.Y + targetCtrl.ActualHeight;
if (menu.Placement != PlacementMode.Bottom)
throw new NotImplementedException("you need to implement your own logic for other modes");
return screen.WorkingArea.Bottom < targetBottom + menu.ActualHeight;
}
Final result:

How to hide Grid border

I have a grid with some controls inside. I want to hide the grid border.
Are there any specific properties which can be used to hide the grid border in WPF?
Set DataGrid.BorderThickness to 0.
I know this is a bit late to add to the thread but incase anyone else stumbles on this question this worked for me:
<Grid ShowGridLines="False">
I too had a similar problem of getting dotted border around a part(Grid) of user control, only when focus is being set to the Grid and solved the problem using;
void Grid_GotFocus (object sender, RoutedEventArgs e) {
Grid.FocusVisualStyle = null;
}
Hope it helps someone :)

WPF Ribbon - Hide quick access toolbar

how do you hide Quick Access Toolbar in a WPF's Ribbon?
For Microsoft Ribbon for WPF, you can hide it by using the VisualTreeHelper. On the Loaded event handler, just resize the row containing the Quick Access Toolbar to 0 :
private void RibbonLoaded(object sender, RoutedEventArgs e)
{
Grid child = VisualTreeHelper.GetChild((DependencyObject)sender, 0) as Grid;
if (child != null)
{
child.RowDefinitions[0].Height = new GridLength(0);
}
}
The Quick Access Toolbar is automatically hidden when the Ribbon control is in a RibbonWindow. When it is not, it seems impossible to hide it. I have already worked hours on this issue and was unable to hide it properly.
But there is one simple workaround: Place the Ribbon control inside of a Panel and give it a negative top margin so it will slide outside of the Panel. Set the Panel's ClipToBounds property to true and the QAT will be hidden.
By the way - there are multiple Ribbon implementations for WPF, even by Microsoft themselves ("Fluent Ribbon" and "Microsoft Ribbon for WPF"), so next time you should mention which one you are talking about.
Or if you want it all in the XAML, this works
<ribbon:Ribbon>
<ribbon:Ribbon.Loaded>CollapseQuickAccessToolbar</ribbon:Ribbon.Loaded>
<x:Code>
private void CollapseQuickAccessToolbar(Object sender, RoutedEventArgs e) {
((Grid)VisualTreeHelper.GetChild((DependencyObject)sender, 0)).RowDefinitions[0].Height = new GridLength(0);
}
</x:Code>
</ribbon:Ribbon>
Here is the solution :
this.ribbonControl1.ToolbarLocation = DevExpress.XtraBars.Ribbon.RibbonQuickAccessToolbarLocation.Hidden;
I know this is an old post, but found an easier solution...
Add this inside the ribbon :-
<ribbon:Ribbon.QuickAccessToolBar>
<ribbon:RibbonQuickAccessToolBar Visibility="Collapsed"/>
</ribbon:Ribbon.QuickAccessToolBar>
Bit late to the party.
<my:Ribbon >
<my:Ribbon.ApplicationMenu >
<my:RibbonApplicationMenu Visibility="Collapsed">
</my:RibbonApplicationMenu>
</my:Ribbon.ApplicationMenu>
This will help to hide the quick bar

Resources