Winforms devexpress tile resize - winforms

I dev WinForms w/ devexpress app, I want to resize and change the color of the tile in the group inside the tileBar.
I found no property to change size/color nor I could find any place to insert code inside C#.
private DevExpress.XtraBars.Navigation.TileBar tileBar1;
private DevExpress.XtraBars.Navigation.TileBarGroup tileBarGroup2;
private DevExpress.XtraBars.Navigation.TileBarGroup tileBarGroup3;
private DevExpress.XtraBars.Navigation.TileBarItem Refresh;
private DevExpress.XtraBars.Navigation.TileBarItem tileBarItem2;
private DevExpress.XtraBars.Navigation.TileBarItem tileBarItem4;
private DevExpress.XtraBars.Navigation.TileBarItem tileBarItem3;
private DevExpress.XtraBars.Navigation.TileBarItem tileBarItem5;
private DevExpress.XtraBars.Navigation.TileBarItem tileBarItem6;
private DevExpress.XtraBars.Navigation.TileBarItem tileBarItem7;
private DevExpress.XtraBars.Navigation.TileBarItem tileBarItem8;
private DevExpress.XtraGrid.GridControl gridControl1;
private DevExpress.XtraGrid.Views.Grid.GridView gridView1;
private DevExpress.XtraGrid.Columns.GridColumn StoreID;
private DevExpress.XtraGrid.Columns.GridColumn EnabledFrom;
private DevExpress.XtraGrid.Columns.GridColumn LastChanged;
private DevExpress.XtraGrid.Columns.GridColumn LastChangedBy;
private DevExpress.XtraBars.Navigation.TileBarItem tileBarItem9;

You can change a background color of a tile by using the TileBarItem.AppearanceItem.Normal.BackColor property.
TileBar supports items of two sizes: medium and wide. To manage height of a medium tile, use the TileBar.ItemSize property. To change width of a wide tile, use the TileBar.WideTileWidth property. To specify if your tile should be medium or wide, use the TileBarItem.ItemSize property.

Related

Second Window Positioning WPF

I have MainWindow which has a button that allows it to open another WPF Window. I want this window to open always on the right hand side of the MainWindow practically right next to it.
How can I do this? This needs to work even if the width of the MainWindow changes as I have various buttons on the MainWindow that can change the size of the MainWindow depending on what panel is visible.
You can calculate where you want the new window if you have a reference to the other window.
Get the other windows position by accessing the Left and Top properties and its width by accessing the ActualWidth or the Width property.
Now you can calculate the new windows position by adding Left + Width + Some spacing.
Check out the documentation for the Left property here:
http://msdn.microsoft.com/en-us/library/system.windows.window.left.aspx
The others behave similarily.
You need to set manual startup location for second window in properties or in code:
WindowStartupLocation = WindowStartupLocation.Manual;
On events Loaded, SizeChanged, LocationChanged of first window, you should adjust position of second window like this:
public void AdjustPosition()
{
window2.Left = Application.Current.MainWindow.Left + Application.Current.MainWindow.ActualWidth;
window2.Top = Application.Current.MainWindow.Top;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
AdjustPosition();
}
private void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
{
AdjustPosition();
}
void MainWindow_LocationChanged(object sender, EventArgs e)
{
AdjustPosition();
}

How do I fire a custom event from a User Control?

I have a very interesting task that I need help with. The description is as follows:
I have a user control (SomeUserControl), that I am using in a Main Window. I write my application entirely on SomeUserControl, and Main Window is hosting SomeUserControl and nothing else.
Right now I have a shutdown button (in SomeUserControl), that is supposed to close the Main Window. The reason for this is that I do not want anything from SomeUserControl to close the application itself, but to fire an event from SomeUserControl, and Main Window receives it, and Main Window will close the application instead of SomeUserControl.
How do I do it? I am not familiar with the concept of creating and handling custom events, so if someone could explain it in words and in code as an example, I will be very grateful to you!
Edit: Here's my code so far.
(in Window 2)
Public Event CloseApp As EventHandler
Private Sub CancelButton_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles CancelButton.Click
DialogResult = False
RaiseEvent CloseApp(Me, New EventArgs)
End Sub
(In Main Window)
Public loginPage As New LoginPage
Public Sub New()
InitializeComponent()
AddHandler loginPage.CloseApp, AddressOf Me.ShutDownJobChangeWindow
End Sub
Private Sub ShutDownJobChangeWindow(ByVal sender As Object, ByVal e As EventArgs)
Application.Current.Shutdown()
End Sub
Goal: I want to close the application when I click cancel in Window 2, but I don't want to do it in such a way that Window 2 closes itself, but by sending some notification to Main Window, and Main Window closes the application.
If the logic of the user control is implemented in the "code behind" of the user control class, do the following.
I'm assuming the XAML file has somewhere a button with a click event:
<Button Click="Button_Click">Close App</Button>
Then, in your code behind for SomeUserControl class, do the following:
public partial class SomeUserControl : UserControl
{
public event EventHandler CloseApp;
...
private void Button_Click( object sender, RoutedEventArgs e )
{
if( CloseApp != null ) {
CloseApp( this, new EventArgs( ) );
}
}
}
In your Main window, listen to the event:
...
someUserCtrl.CloseApp += new EventHandler( MyFn );
private void MyFn( object sender, object EventArgs e ) {
...
}
The simplest way to do this is to declare a custom event:
public delegate void ShutdownEventHandler (object sender, EventArgs data);
public event ShutdownEventHandler Shutdown;
protected void OnShutdown(EventArgs args)
{
if(Shutdown != null)
Shutdown(this, args);
}
Then you subscribe to the Shutdown event in MainWindow, and handle shutdown logic there.
And in SomeUserControl, you simply run OnShutdown when you want to shutdown your application.
I had the same problem, and I've solved in this way:
if you are using a soft version of MVVM (with soft I mean that you use codebehind for events handling) and your event is within the ModelView class do this:
In your MainWindow:
public MainWindow(ViewModels.MyViewModel vm)
{
InitializeComponent();
//pass the istance of your ViewModel to the MainWindow (for MVVM patter)
this.vm = vm;
//Now pass it to your User Control
myUserControl.vm = vm;
}
In your UserControl
public partial class MyUserControl: UserControl
{
public ViewModels.MyViewModel vm;
public MyUserControl()
{
InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
vm.MyMethod();
}
}
Last but not least, in your MainWindow's xaml insert your user control and give it a name:
<local:MyUserControl x:Name="myUserControl" />
If you don't want to use a ViewModel, you can simply pass to your UserControl an instance of your MainWindow and then you can run within the codebehind of your usercontrol all of your MainWindow's public methods.
Hope it will help!
Sorry, the question is VB so here is the VB version of the code above:
MainWindow:
Public Sub New(ByVal vm As ViewModels.MyViewModel)
InitializeComponent()
Me.vm = vm
myUserControl.vm = vm
End Sub
UserControl:
Public Partial Class MyUserControl
Inherits UserControl
Public vm As ViewModels.MyViewModel
Public Sub New()
InitializeComponent()
End Sub
Private Sub button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
vm.MyMethod()
End Sub
End Class

Public vs Private AttachedProperties

Where does it make sense to have AttachedProperties as private vs public?
Usually it is define as (example):
public static readonly DependencyProperty CommandProperty =
DependencyProperty.RegisterAttached(
"Command",
typeof(ICommand),
typeof(Click),
new PropertyMetadata(OnSetCommandCallback));
But I have also seen examples where some properties are private static readonly...
What are the consequences if I change the above CommandProperty to private now? It seems to be still available in my XAML intellisense if I do that. What am I missing here?
The difference is that you won't be able to access the DependencyProperty from outside of the class. This can make sense if the static Get and Set methods are private as well, (in an attached behavior where you need to store some behavior local data for example) but not otherwise (and I don't think I've ever seen this with public Get and Set).
An example of when you want to use the DependencyProperty is DependencyPropertyDescriptor. With a public DependencyProperty you can do the following
DependencyPropertyDescriptor de =
DependencyPropertyDescriptor.FromProperty(Click.CommandProperty, typeof(Button));
de.AddValueChanged(button1, delegate(object sender, EventArgs e)
{
// Some logic..
});
But if the DependencyProperty is private, the above code won't work.
However, the following will work fine for both a public and private DependencyProperty (if the static Get and Set methods are public) since the owner class can access the private DependencyProperty. This also goes for Bindings and values set through Xaml where GetValue and SetValue are called directly.
Click.SetCommand(button, ApplicationCommands.Close);
ICommand command = Click.GetCommand(button);
If you look through the framework you will notice that all of the public attached properties have a public DependencyProperty, for example Grid.RowProperty and Storyboard.TargetNameProperty. So if the attached property is public, use a public DependencyProperty

How is "Esc" key handled in WPF Window?

I want the Escape key to close my WPF window. However if there is a control that can consume that Escape key, I don't want to close the Window. There are multiple solutions on how to close the WPF Window when ESC key is pressed. eg. How does the WPF Button.IsCancel property work?
However this solution closes the Window, without regard to if there is an active control that can consume the Escape key.
For eg. I have a Window with a DataGrid. One of the columns on the dataGrid is a combobox. If I am changing the ComboBox, and hit Escape, then the control should come out of editing of the comboBox (Normal Behavior). And if I now hit Escape again, then the Window should close. I would like a generic solution, instead of writing a lot of custom code.
If you can provide a solution in C# it would be great.
You should just use the KeyDown event instead of the PreviewKeyDown event. If any child of the Window handles the event, it won't be bubbled up to the Window (PreviewKeyDown tunnels from the Window down), and therefore your event handler won't be called.
There may be an easier way, but you could do it with the hash code. Keys.Escape is another option, but sometimes I cannot get that to work for some reason. You didn't specify a language so here is an example in VB.NET:
Private Sub someTextField_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles someTextField.KeyPress
If e.KeyChar.GetHashCode = 1769499 Then ''this number is the hash code for escape on my computer, do not know if it is the same for all computers though.
MsgBox("escape pressed") ''put some logic in here that determines what ever you wanted to know about your "active control"
End If
End Sub
class Commands
{
static Command
{
CloseWindow = NewCommand("Close Window", "CloseWindow", new KeyGesture(Key.Escape));
CloseWindowDefaultBinding = new CommandBinding(CloseWindow,
CloseWindowExecute, CloseWindowCanExecute);
}
public static CommandBinding CloseWindowDefaultBinding { get; private set; }
public static RoutedUICommand CloseWindow { get; private set; }
static void CloseWindowCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = sender != null && sender is System.Windows.Window;
e.Handled = true;
}
static void CloseWindowExecute(object sender, ExecutedRoutedEventArgs e)
{
((System.Windows.Window)sender).Close();
}
}
// In your window class's constructor. This could also be done
// as a static resource in the window's XAML resources.
CommandBindings.Add(Commands.CloseWindowDefaultBinding);

Hide WPF elements in Visual Studio designer

I have a WPF form which basically looks like this:
<Window ...>
<Grid>
<DockPanel>
[content shown during normal operation]
</DockPanel>
<Grid Background="#CCCC" Visibility="Hidden">
[overlay grid which is only shown during special circumstances]
</Grid>
</Grid>
</Window>
The overlay grid hides everything else (i.e. the "normal content") and is only shown under special circumstances (i.e. if the network connection goes down). This works perfectly fine when running the program.
Now, in design mode, the problem is that Visual Studio ignores the Visibility="Hidden". Usually, this makes perfect sense (after all, I want to be able to edit the hidden UI elements), but in my case it's annoying, because it prevents me from editing the stuff in the DockPanel in the designer.
So, what I'd like to do is something like that:
<Grid Background="#CCCC" Visibility="Hidden" VS.ShowInDesigner="False">
[overlay grid which is only shown during special circumstances]
</Grid>
But, alas, there is no such property, or at least none that I know of. Any ideas?
Starting from VS2012 you can just use the Blend namespace IsHidden attribute:
add if not already present xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
put d:IsHidden="true" on element you want to hide at design time only
Nice solution, I was having a similar problem and I agree that there are cases where it's needed. Here is a minor update that allows you to edit the value to turn IsHidden on and off while designing. I also applied a ScaleTransform instead of setting Width and Height to reduce screen artifacts a bit if control grips etc are displayed and to avoid conflicts if the control being hidden already has Width and Height properties set (assuming that the control doesn't already have a LayoutTransform set on it).
Public Class DesignModeTool
Public Shared ReadOnly IsHiddenProperty As DependencyProperty = DependencyProperty.RegisterAttached( _
"IsHidden", GetType(Boolean), GetType(DesignModeTool), _
New FrameworkPropertyMetadata(False, New PropertyChangedCallback(AddressOf OnIsHiddenChanged)))
Public Shared Sub SetIsHidden(ByVal element As FrameworkElement, ByVal value As Boolean)
element.SetValue(IsHiddenProperty, value)
End Sub
Public Shared Function GetIsHidden(ByVal element As FrameworkElement) As Boolean
Return DirectCast(element.GetValue(IsHiddenProperty), Boolean)
End Function
Private Shared Sub OnIsHiddenChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
If System.ComponentModel.DesignerProperties.GetIsInDesignMode(d) AndAlso True.Equals(e.NewValue) Then
With DirectCast(d, FrameworkElement)
.LayoutTransform = New ScaleTransform(0.001, 0.001)
End With
ElseIf System.ComponentModel.DesignerProperties.GetIsInDesignMode(d) AndAlso False.Equals(e.NewValue) Then
With DirectCast(d, FrameworkElement)
.LayoutTransform = Nothing
End With
End If
End Sub
End Class
Nice work! I translated to C# and change the property it's changing to RenderTransform.
static class DesignModeTool
{
public static readonly DependencyProperty IsHiddenProperty =
DependencyProperty.RegisterAttached("IsHidden",
typeof(bool),
typeof(DesignModeTool),
new FrameworkPropertyMetadata(false,
new PropertyChangedCallback(OnIsHiddenChanged)));
public static void SetIsHidden(FrameworkElement element, bool value)
{
element.SetValue(IsHiddenProperty, value);
}
public static bool GetIsHidden(FrameworkElement element)
{
return (bool)element.GetValue(IsHiddenProperty);
}
private static void OnIsHiddenChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
if (!DesignerProperties.GetIsInDesignMode(d)) return;
var element = (FrameworkElement)d;
element.RenderTransform = (bool)e.NewValue
? new ScaleTransform(0, 0)
: null;
}
}
Since there is no built-in way to do this, I decided to implement a solution myself, which was surprisingly easy to do using attached properties:
Public Class DesignModeTool
Public Shared ReadOnly IsHiddenProperty As DependencyProperty = DependencyProperty.RegisterAttached( _
"IsHidden", GetType(Boolean), GetType(DesignModeTool), _
New FrameworkPropertyMetadata(False, New PropertyChangedCallback(AddressOf OnIsHiddenChanged)))
Public Shared Sub SetIsHidden(ByVal element As UIElement, ByVal value As Boolean)
element.SetValue(IsHiddenProperty, value)
End Sub
Public Shared Function GetIsHidden(ByVal element As UIElement) As Boolean
Return DirectCast(element.GetValue(IsHiddenProperty), Boolean)
End Function
Private Shared Sub OnIsHiddenChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
If System.ComponentModel.DesignerProperties.GetIsInDesignMode(d) AndAlso True.Equals(e.NewValue) Then
With DirectCast(d, FrameworkElement)
.Width = 0
.Height = 0
End With
End If
End Sub
End Class
After declaring a namespace, the feature can be used like this:
<Grid ... local:DesignModeTool.IsHidden="True">
[stuff I don't want to be shown in the designer]
</Grid>
Other than not using the designer (really, consider this) you could separate the contents of the Grid into a separate UserControl. That way, you could just update that UserControl in isolation from the visibility logic.
I ran into a similar problem recently.
I am using a Rectangle to obscure the main window during a modal dialog's execution. I have the Visibility data bound, but the Rectangle made the designer unusable. I mad the Z index a one time data bind, and a fallback value was lower than the window I wanted to obscure. When the application starts up, the Rectangle's Z index is bound to a higher value than the window.
I am on the other side... hate VS 2012 for hiding hidden WPF controls in designer. I need to see them so i have modified gregsdennis code to:
public class DesignModeTool
{
public static readonly DependencyProperty IsHiddenProperty = DependencyProperty.RegisterAttached("IsHidden", typeof(bool), typeof(DesignModeTool), new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnIsHiddenChanged)));
public static void SetIsHidden(FrameworkElement element, bool value)
{
element.SetValue(IsHiddenProperty, value);
}
public static bool GetIsHidden(FrameworkElement element)
{
return (bool)element.GetValue(IsHiddenProperty);
}
private static void OnIsHiddenChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
if (!DesignerProperties.GetIsInDesignMode(d)) return;
var element = (FrameworkElement)d;
element.Visibility=Visibility.Visible;
}
}
wpfClasses2:DesignModeTool.IsHidden="False" will show the control in designer mode.

Resources