Style WindowsForms Project - winforms

I want to style my forms and some controls on my project, e.g. dataGridView.
I know, I can make a method like this an set it for every dataGridView.
public static void StyleDataGridView(DataGridView dg)
{
dg.BorderStyle = BorderStyle.FixedSingle;
dg.RowHeadersVisible = false;
....
}
My question: is there a possibility to do such style code only once in my project? How?

First step with userControl worked very well.
But how to handle the Click, DoubleClick and the CellMouseDown Event?
My code so far:
public partial class ucGridView : UserControl
{
public object DataSource
{
get { return dataGridView.DataSource; }
set { dataGridView.DataSource = value; }
}
public ucGridView()
{
InitializeComponent();
}
}

Related

How to exit from wpf app

I have a WPF project that is part of the solution, it is "ProjectFilesSelector". A some project, named A. call ProjectFilesSelector as figure below:
!!! UPDATED:
namespace ProjectFilesSelector
{
...
public class ViewModel
{
...
public ICommand cancel
{
get
{
return new WPFExtensions.RelayCommand(_ =>
{
this.window.Visibility = Visibility.Hidden;
this.window.Close();
});
}
}
}
public partial class Window1 : Window, IDisposable
{
public Window1(ProjectTypes.Project pro)
{
InitializeComponent();
var context = new ViewModel(this, new ATChecker.ViewModel.ProjectModel(pro));
this.DataContext = context;
}
...
}
}
namespace ATCheckerView
{
public class ViewerClientExt : INotifyPropertyChanged
{
...
public ICommand CheckPrinciplies
{
get
{
var cmnd =
new RelayCommand(project =>
{
var proj = (ViewModel.ProjectModel)project;
ProjectFilesSelector.ViewModel dc;
using (var a = new ProjectFilesSelector.Window1(proj.project))
{
a.ShowDialog(); // cancel command was called
dc = (ProjectFilesSelector.ViewModel)a.DataContext;
}
....
// some code
// and I can still see the window of Window1. Why?
});
I don t think you want to exit the application. You may just want to close the window.
The cancel button just set IsCancel to true. And for the ok button the best way is to create a event in the viewmodel to get up to the view.

override the super class methods in wpf Window

I wanna make a customized window base. So I made a custom window which inherits from Window.
For example:
public class MyWindowBase : Window
{
...
...
}
I wanna override the different Brushes of the super class Window for my own purpose.
In my previous experience, to override methods/properties with no abstract or virtual in the super class need the key word "new".
For example:
public new void DoSomething() { ........ base.DoSomething() ....... }
public new string SomeText { get { ... } set {......} }
It works in my previous work.
However, in this time of dealing with WPF Window, it doesn't work.
I tried to override the different Brushes of the super class Window as follows:
public new Brush BorderBrush
{
get { ... }
set { myBorderBrush = value; base.BorderBrush = null }
}
public new Brush Background
{
get { ... }
set { myBackground = value; base.Backgound = null; }
}
.....
.....
.....
I tried the change the value of the above Brushes in MyWindowBase, it just change the value of the super class Window, it doesn't change the value of myBorderBrush and myBackground.
So, how could I override the methods and properties of the super class Window?
Actually, I want to override the base background so that it will be null or transparent forever, but the changed value will be applied on my own custom Background.
Thank you very much!
If you only want to set the value, then you can set it using
this.BorderBrush = Brushes.Blue;
this.Background = Brushes.Red;
If you wish to overwrite the Property Metadata (for things like default value, property changed logic, or validation), you can use OverrideMetadata
Window.BackgroundProperty.OverrideMetadata(
typeof(MyWindowBase), myPropertyMetadata);
If you simply want to add some logic on Changed, you can use a DependencyPropertyDescriptor
var dpd = DependencyPropertyDescriptor.FromProperty(
Window.BackgroundProperty, typeof(Window));
dpd.AddValueChanged(this.Value, new EventHandler(BackgroundChanged));
private void BackgroundChanged(object sender, EventArgs e)
{
//do the code here
}
And if you're looking to override a Method, and not a Property, then you can use the override keyword
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
}

Open new Window from view model

Hi I have a beginner problem. I have shell (it is wpf window) and in this shell is screen (it is an user control / view model).
I would like open new window from view model, not show user control in shell.
So I create new window - ChatView
<Window x:Class="Spirit.Views.ChatView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:extToolkit="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit.Extended" Title="ChatView" Height="545" Width="763">
<Grid Margin="4,4,4,4">
</Grid>
</Window>
Export ChatViewModel with MEF.
public interface IChatViewModel
{
}
[Export("ChatScreen",typeof(IChatViewModel))]
public class ChatViewModel
{
}
In view model I have this method:
With ShowScreen class help me Mr.Marco Amendola. It look likes this:
public class ShowScreen : IResult
{
readonly Type _screenType;
readonly string _name;
[Import]
public IShellViewModel Shell { get; set; }
Action<object> _initializationAction = screen => { };
public ShowScreen InitializeWith<T>(T argument)
{
_initializationAction = screen =>
{
var initializable = screen as IInitializable<T>;
if (initializable != null)
initializable.Initialize(argument);
};
return this;
}
public ShowScreen(string name)
{
_name = name;
}
public ShowScreen(Type screenType)
{
_screenType = screenType;
}
public void Execute(ActionExecutionContext context)
{
var screen = !string.IsNullOrEmpty(_name)
? IoC.Get<object>(_name)
: IoC.GetInstance(_screenType, null);
_initializationAction(screen);
Shell.ActivateItem(screen);
Completed(this, new ResultCompletionEventArgs());
}
public event EventHandler<ResultCompletionEventArgs> Completed = delegate { };
public static ShowScreen Of<T>()
{
return new ShowScreen(typeof(T));
}
}
My problem is if I try show new window it doesn’t works, it works only if I show new user control in shell(window).
I would like achieve behavior something like in skype. You have a main window with listbox, you double clicked on item and it show new chat window.
Main window can publish with EventAggregator on chat window and also chat window can publish on main window. This is my goal.
I know that I can not use class ShowScreen on showing new Window. I would like to know what is correct way to create new window from view model and inject event aggregator
to this vie model.
Any advice? Thank for your help and time.
Have you looked at WindowManager.Show or WindowManager.ShowDialog? Rob has a sample at http://caliburnmicro.codeplex.com/wikipage?title=The%20Window%20Manager. You can inject this dependency into your view model as IWindowManager.
I'm using this. Maybe could save a question about "where's the code ?".
DialogHelper:
public class DialogHelper
{
public void ShowDialog<T>(params Object[] param) where T : class
{
var windowManager = new WindowManager();
T viewModel = Activator.CreateInstance(typeof(T), param) as T;
windowManager.ShowWindow(viewModel);
}
}
How to use:
Without constructor parameter:
Dialog.ShowDialog<TestTableViewModel>();
With constructor paramater:
Dialog.ShowDialog<TestTableViewModel>(dt);
Note that I'm not using MEF

Silverlight View exporting DependencyProperty to ViewModel via MEF

I have a need of one DependencyProperty from a View in my ViewModel constructor:
My problem: MEF wouldn't SatisfyImports() 'because it is marked with one or more ExportAttributes' (that is the exception)
This is the code structure for the VIEW:
public class MyView : UserControl
{
[Export(MethodTypes.ChartType)]
public Charts MyChartType
{
get
{
object k = GetValue(ChartTypeProperty);
Charts f = (Charts)Enum.Parse(typeof(Charts), k.ToString(), true);
return f;
}
set
{
SetValue(ChartTypeProperty, value);
}
}
[Import(ViewModelTypes.GenericChartViewModel)]
public object ViewModel
{
set
{
DataContext = value;
}
}
public MyView()
{
InitializeComponent();
if (!ViewModelBase.IsInDesignModeStatic)
{
// Use MEF To load the View Model
CompositionInitializer.SatisfyImports(this);
}
}
}
and the VIEWMODEL:
[PartCreationPolicy(CreationPolicy.NonShared)]
[Export(ViewModelTypes.GenericChartViewModel)]
public class GenericChartViewModel
{
[ImportingConstructor]
public GenericChartViewModel([Import(MethodTypes.ChartType)] Charts forChartType)
{
string test = forChartType.ToString();
}
}
Please give me any hints on this or maybe suggest a better solution for passing parameters through mef
In my case, I would need to pass only dependecyproperty's for now...
Thanks
Your work around isn't really good.. can't you remove the export from ChartTypes and pass it manually to whoever wants it? I presume the viewmodel is only one insterested in it..
I managed to put this through !
Instead of the code in the default constructor, I use:
void MyView_Loaded(object sender, RoutedEventArgs e)
{
if (!ViewModelBase.IsInDesignModeStatic)
{
var catalog = new TypeCatalog(typeof(GenericChartViewModel));
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
}
}
and the dependencyproperty value is correctly propagated to the ViewModel
(must do this after control is loaded, or the property will have its default value)
However, I would be very grateful if someone could:
tell me how generate a catalog from another non-referenced assembly?
Thanks

How to set form object property globally in .net winforms?

i am using .net win forms i need to set some common properties globally to my win forms like css in web application
ex
form background color=red
button width =100
Text box width=200
font family=arial
how to do this?
how about create a base form that all the other forms inherit.
On the base form you can set the common look and feel.
then if it is necessary to overwrite the common properties you can do so.
EDIT
something like this for the base form.
public partial class BaseForm : Form
{
private Font _font = new Font("Arial", 10);
private Color _backColor = Color.Red;
public BaseForm()
{
InitializeComponent();
}
public override Font Font
{
get { return _font; }
set { _font = value; }
}
public override Color BackColor
{
get { return _backColor; }
set { _backColor = value; }
}
}
and this for the form that you want to display
public partial class Form1 : BaseForm
{
public Form1()
{
InitializeComponent();
}
}
Use the App.Config file or the Settings tab in project properties.
You could create a static class to store them - maybe in a Dictionary perhaps.
Something like this could work:
public static class GlobalData
{
private static Dictionary<string, object> settings;
private static void SetDefaults()
{
settings = new Dictionary<string, object>();
settings.Add("BackgroundColour", "Red");
settings.Add("Width", 100);
}
public static Dictionary<string, object> FormSettings
{
get {
if (settings ==null)
{
SetDefaults();
}
return settings;
}
}
}
EDIT:
You could you use it like this:
this.Width = Convert.ToInt32(GlobalData.FormSettings["Width"].ToString());

Resources