|
I'm using MahApps in my app. The data grid's column header style looks like this.
So I added this to my cell headers:
<DataGridTextColumn.HeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="HorizontalContentAlignment"
Value="Center" />
</Style>
</DataGridTextColumn.HeaderStyle>
The problem is that now the column headers have lost their style. It now looks like this
How can I apply the header alignment without loosing my grid styling?
Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Use BasedOn.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
So I have this ListView , and I want to make a specific column have a different font family, so I tried setting the CellTemplate for the column. Because of the architecture of the app, I have to apply this template in the C# code.
I tried :
- Creating a resource like so:
<DataTemplate x:Key="PAScodeGridCell" >
<TextBlock Text="{Binding}" FontFamily="Consolas" />
</DataTemplate>
and was setting it like so:
column.CellTemplate = (DataTemplate)WpfCommon.Globals.XamlReaderLoad("PAScodeGridCell"); It found the template in the resource file, and set it to the CellTemplate property, but the font in that column wasn't Consolas.
Next, I tried so I tried specifying the template as a string in the c# code like so:
readonly string _CELL_TEMPLATE_ = string.Concat( "<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">",
"<TextBlock ",
"Text=\"{{Binding Path=PAScode}}\" ",
"FontFamily=\"Consolas\" /></DataTemplate>", and set the property like so
column.CellTemplate = (DataTemplate)XamlReader.Load(xaml);
The result was no different.
By all rights, both methods should have worked. I inspected the visual tree while the app was running and it's as if the specified template wasn't applied at all.
What am I don't wrong?
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
|
I think I posted the wrong snippet for the first attempt. I used FindResource there.
No matter which way I tried it, the CellTemplate showed that it was set, but the column did not reflect the settings I was trying to set.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
I found the problem. This is the code that was creating the GridViewColumn:
DataTemplate cellTemplate =
Binding binding = new Binding()...
GridViewColumn column = new GridViewColumn()
{
Header = "...",
DisplayMemberBinding = binding,
CellTemplate = cellTemplate,
};
I had to change it to NOT set the DisplayMemberBinding property:
GridViewColumn column = new GridViewColumn()
{
Header = "...",
DisplayMemberBinding = (celltable == null) ? binding : null,
CellTemplate = cellTemplate,
};
And now it does exactly what I want it to do.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
Glad it's working.
Might be a timing thing:
Quote: Resources can also be referenced by code from within the collection, but be aware that resources created in XAML will definitely not be accessible until after Loaded is raised by the element that declares the dictionary.
(I see now you're "creating" on the fly; I was "switching" when I looked back at my app).
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
It's a binding thing - I was using DisplayMemberBinding, and then trying to apply a template that did its own binding - you can't do both, and when you try, it always does the DisplayMemberBinding first.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
That makes sense.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
I have an icon (resource, do not copy) in my project.
I added an Image element to my xaml.
<Image Source="pack://application,,,/Pilot128.ico" Height="24" />
The icon shows up in the designer, but not when I actually run the app.
However, when I do this, it works as expected:
<Window.Resources>
<BitmapImage x:Key="appIcon" UriSource="Pilot128.ico" />
</Window.Resources>
<Image Source="{StaticResource appIcon}" Height="24" />
Why doesn't the first way work?
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
Have you tried either:
<Image Source="/Pilot128.ico" Height="24" /> or:
<Image Source="pack://application:,,,/YourAssemblyName;component/Pilot128.ico" Height="24" /> The documentation[^] seems to suggest that your URI should work, but I don't think I've ever managed to make it work.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Yeah, they both do the same thing.
I guess I'm fine with what I have to do to get this to work, but it doesn't make any sense that I *have* to do that way, unless it's because it's an ICO file as opposed to a png/jpg?
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
|
When does the next tick start. Watching how things seem to preform it seems like:
Tick
Execute Tick routine, pause tick timer
Tick routine ends, restart tick timer
It seems like the tick interval is between the tick routines. It is OK, I just want to make sure I understand it correctly.
So many years of programming I have forgotten more languages than I know.
|
|
|
|
|
You have the sequence wrong!
Tick
Stop the timer
process the tick routine
restart the timer.
If your process is longer than the timer interval it clags up the system.
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
That's what I meant. Maybe I did not state it well.
"it clags up the system" is what I thought at first but it seems to follow the above sequence fairly well. As I change the tick interval it seems to be offset by a constant. My tick routine has to read some USB analog inputs. It is slow but consistent.
I cannot find anything on the web to explain clags.
So many years of programming I have forgotten more languages than I know.
|
|
|
|
|
Sorry - slang
You end up with queued processes that are re-launched before the last one is completed therefore you need to insure that only 1 process is launched at a time.
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
I like the slang better.
So many years of programming I have forgotten more languages than I know.
|
|
|
|
|
You only need to pause, if the tick event takes longer to execute than the interval if you want a "proper" timer.
And if you're re-entrant, you don't need to pause either.
Pausing implies your interval is "off".
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
In my current way to structure a WPF-application I heavily lean on UserControls for my Views and place them in the MainWindow inside a container, usually a DockPanel. For instance in the MainWindow:
<DockPanel LastChildFill="True">
<views:Header DockPanel.Dock="Top" />
<views:Footer DockPanel.Dock="Bottom" />
<views:Body />
</DockPanel> For each View I have a corresponding ViewModel. This ViewModel is instantiated inside the View's XAMl-code. In the DataContext element. For instance in the Body View:
<UserControl.DataContext>
<vm:BodyViewModel />
</UserControl.DataContext> I like this approach, because I can alter any properties in the ViewModel and see the View automatically change in the view during design time.
But I have not find a good way to access any ViewModels from some class where I happened to need to alter the View through a property in a ViewModel.
My current solution - which I don't like - to gain access of the ViewModels is:
1. Give each View a name in the MainWindow.xaml file.
<DockPanel LastChildFill="True">
<views:Header DockPanel.Dock="Top" x:Name="viewHeader" />
<views:Footer DockPanel.Dock="Bottom" x:Name="viewFooter" />
<views:Body x:Name="viewBody" />
</DockPanel> 2. Do some logic in the MainWindow to setup the ViewModels for each View.
public partial class MainWindow : Window
{
HeaderViewModel Header { get; set; }
FooterViewModel Footer { get; set; }
BodyViewModel Body { get; set; }
public MainWindow()
{
InitializeComponent();
Header = viewHeader.DataContext as HeaderViewModel;
Footer = viewFooter.DataContext as FooterViewModel;
Body = viewBody.DataContext as BodyViewModel;
}
} 3. Then for every class where I need a ViewModel I need to pass it from the MainWindow in some way.
class SomeClass
{
public void AlterHeaderText(HeaderViewModel header)
{
}
} This approach works, but I don't like it. It's annoying to pass ViewModels from the MainWindow every time a class need one or more ViewModels to alter a View.
Is there a solution to this approach to improve it?
/BR
Steffe
|
|
|
|
|
You're accessing the UC's directly: tight coupling.
Ideally, you should be communicating only with the Main Window; using methods / an interface that works indirectly with the UC's; e.g. "set heading title".
Use a static (e.g. .Current) to access "main window" and any other windows / UC's that only exist as singletons (at any given time).
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
If you have a viewmodel for each user control, why is the setup performed in the main window anything more than adding the user control to the parent content control (a grid?)? The view should do all of its own setup, and should betotally self-contained at that point. Sure, you may have some sort of global data, but in the end, the user control and its viewmodel should really be completely self-contained, and the main window shouldn't even be involved.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
Look at MVVM Light or its soom to be replacement Microsoft.Toolkit.MVVM. They have there place but personally I think they can make simple things more complicated.
So many years of programming I have forgotten more languages than I know.
|
|
|
|
|
Historically (and in my experience), Microsoft's "convenience" frameworks are too generic to be even remotely useful. And the closer you get to an enterprise-sized application, the worse it gets.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
You could use singletons. I posted an article that uses singletons for app settings, the connection string for a database, and the DAL object I use to access the database. If you look at the source code, you'll see how to do it (or at least, how I did it).
Entity Factory - Get Your ORM-less Freak On![^]
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|