|
When I do this type of thing, I use MVVM and have a state represent the loaded nature; the visibility of the spinner animation is bound to the loaded state.
|
|
|
|
|
Let me clarify my question... I'm not asking about the indicator...
- Tab appears.
- Spinning indicator appears
- Call GetData
- Data is returned
- Tab's content is loaded
- Indicator goes away.
The problem is that I want to show a blank tab first, then start the process of getting the data. But where do you start it from? There doesn't seem to be a ContentRedered or Loaded or Initialized event to kick off the async data call after the tab is visible
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
I can't tell whether you are using MVVM or not from your question. If I were doing this, I would adopt a slightly different approach; I would bind the tabs to an ObservableCollection. When I instantiate my new tab and add it to the collection, I would trigger the async load. In other words, I disconnect the state from the visual. I don't need to worry about tracking tab states because this is automatically connected to my ViewModel.
|
|
|
|
|
TabItem inherits from FrameworkElement and therefore has a "Loaded" event.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
When you design a CustomControl or UserControl, how do you fire off a method inside it?
Say for example you create a control that needs to load some data async. So you put the control on a Window and start the window. There's no way to call into the control and call 'Load()'.
You can bind to it, but what would you bind? You could have it listen to an event. Is there some other way?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
modified 31-Dec-22 14:45pm.
|
|
|
|
|
You can use async but you may be on a different thread to the UI when trying to update data bound to the UI. You need to marshall to the UI thread or you will get cross-thread exceptions. My previous answer where I point to a series of WPF YT videos will answer this question for you.
Graeme
"I fear not the man who has practiced ten thousand kicks one time, but I fear the man that has practiced one kick ten thousand times!" - Bruce Lee
|
|
|
|
|
Not sure what you mean by this response.
I'm asking how to create a UserControl or CustomControl, place it on a Window, then execute a method on it from a button click on the Window.
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Kevin Marois wrote: I'm asking how to create a UserControl or CustomControl, place it on a Window, then execute a method on it from a button click on the Window.
Okay, that is a lot clearer and easier to understand what you want.
I wrote an article that walks through extending a Control: Flexible WPF ToggleSwitch Lookless Control in C# & VB[^]. UserControls are just like another Window to use.
Graeme
"I fear not the man who has practiced ten thousand kicks one time, but I fear the man that has practiced one kick ten thousand times!" - Bruce Lee
|
|
|
|
|
|
I'm not asking about events. I'm asking how to call the Load method on my UserControl from the Window's ViewModel. The UserControl uis a stand along control. It needs to have Load called to load up the data. The Window's ViewModel doesn't know about the UserControl.
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
You give the user control a .Name in the XAML; the .Name is available to the Window's code behind. If you want to access the UC outside of the Window, you need to add a public reference to the UC; e.g. public Type UC => (name of UC in XAML).
In those cases where the UC is a (global) singleton, I (may) give it a public static reference to its instance; the UC can then be accessed by any part of the app in that case.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
There is no code behind. I'm using MVVM
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
You could have said that at the outset: "I will only entertain MVVM solutions". Noted.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
Sorry for the confusion. I just asumed that MVVM is the way most folks do things.
So I found out a way to load my control:
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
Load();
}
Not sure if this is the best way, but it works.
Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
I'm having trouble using Path data for an image. I followed the accepted answer here, but I'm still having issues.
<Button Height="50"
Width="50">
<Viewbox Stretch="Uniform">
<Canvas Height="122.88"
Width="122.88">
<Path Fill="Red"
Data="M73.48,15.84A46.87,46.87,0,0,1,84.87,21L91,14.84a7.6,7.6,0,0,1,10.72,0L108,21.15a7.6,7.6,0,0,1,0,10.72l-6.6,6.6a46.6,46.6,0,0,1,4.34,10.93h9.52A7.6,7.6,0,0,1,122.88,57V65.9a7.6,7.6,0,0,1-7.58,7.58h-9.61a46.83,46.83,0,0,1-4.37,10.81L108,91a7.6,7.6,0,0,1,0,10.72L101.73,108A7.61,7.61,0,0,1,91,108l-6.34-6.35a47.22,47.22,0,0,1-11.19,5v8.59a7.6,7.6,0,0,1-7.58,7.58H57a7.6,7.6,0,0,1-7.58-7.58v-7.76a47.39,47.39,0,0,1-12.35-4.68L31.87,108a7.62,7.62,0,0,1-10.72,0l-6.31-6.31a7.61,7.61,0,0,1,0-10.72l4.72-4.72A47.38,47.38,0,0,1,14,73.48H7.58A7.6,7.6,0,0,1,0,65.9V57A7.6,7.6,0,0,1,7.58,49.4h6.35a47.2,47.2,0,0,1,5.51-12.94l-4.6-4.59a7.62,7.62,0,0,1,0-10.72l6.31-6.31a7.6,7.6,0,0,1,10.72,0l5,5A46.6,46.6,0,0,1,49.4,15V7.58A7.6,7.6,0,0,1,57,0H65.9a7.6,7.6,0,0,1,7.58,7.58v8.26ZM59.86,36.68a24.6,24.6,0,1,1-24.6,24.59,24.59,24.59,0,0,1,24.6-24.59Z" />
</Canvas>
</Viewbox>
</Button>
If I change the size of the Canvas, the image skews off to the side.
How can I get the Gear icon to be a fixed size and stay centered regardless of the button or canvas size?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
If you want to "fix the size" of the path figure, set the Width and Height on the Viewbox (otherwise it stretches or shrinks).
You could probably do away with Canvas alltogether in this case (coordinates can operate in "virtual space").
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
OK, so here's what I have now:
<Button Height="25"
Width="75">
<Viewbox Stretch="Uniform"
Height="20"
Width="20">
<Canvas Height="122.88"
Width="122.88">
<Path Fill="Red"
Data="M73.48,15.84A46.87,46.87,0,0,1,84.87,21L91,14.84a7.6,7.6,0,0,1,10.72,0L108,21.15a7.6,7.6,0,0,1,0,10.72l-6.6,6.6a46.6,46.6,0,0,1,4.34,10.93h9.52A7.6,7.6,0,0,1,122.88,57V65.9a7.6,7.6,0,0,1-7.58,7.58h-9.61a46.83,46.83,0,0,1-4.37,10.81L108,91a7.6,7.6,0,0,1,0,10.72L101.73,108A7.61,7.61,0,0,1,91,108l-6.34-6.35a47.22,47.22,0,0,1-11.19,5v8.59a7.6,7.6,0,0,1-7.58,7.58H57a7.6,7.6,0,0,1-7.58-7.58v-7.76a47.39,47.39,0,0,1-12.35-4.68L31.87,108a7.62,7.62,0,0,1-10.72,0l-6.31-6.31a7.61,7.61,0,0,1,0-10.72l4.72-4.72A47.38,47.38,0,0,1,14,73.48H7.58A7.6,7.6,0,0,1,0,65.9V57A7.6,7.6,0,0,1,7.58,49.4h6.35a47.2,47.2,0,0,1,5.51-12.94l-4.6-4.59a7.62,7.62,0,0,1,0-10.72l6.31-6.31a7.6,7.6,0,0,1,10.72,0l5,5A46.6,46.6,0,0,1,49.4,15V7.58A7.6,7.6,0,0,1,57,0H65.9a7.6,7.6,0,0,1,7.58,7.58v8.26ZM59.86,36.68a24.6,24.6,0,1,1-24.6,24.59,24.59,24.59,0,0,1,24.6-24.59Z" />
</Canvas>
</Viewbox>
</Button>
The Gear image now is halfway hidden below.
Here's what it looks like now
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
No, I'm not seeing that. You're either inheriting something; or it's a version thing. I tested it with a new WPF .NET 6.0 project.
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow"
Height="450"
Width="800">
<Grid>
<Button Height="25"
Width="75">
<pre>
<Viewbox Stretch="Uniform"
Height="20"
Width="20">
<Canvas Height="122.88"
Width="122.88">
<Path Fill="Red"
Data="M73.48,15.84A46.87,46.87,0,0,1,84.87,21L91,14.84a7.6,7.6,0,0,1,10.72,0L108,21.15a7.6,7.6,0,0,1,0,10.72l-6.6,6.6a46.6,46.6,0,0,1,4.34,10.93h9.52A7.6,7.6,0,0,1,122.88,57V65.9a7.6,7.6,0,0,1-7.58,7.58h-9.61a46.83,46.83,0,0,1-4.37,10.81L108,91a7.6,7.6,0,0,1,0,10.72L101.73,108A7.61,7.61,0,0,1,91,108l-6.34-6.35a47.22,47.22,0,0,1-11.19,5v8.59a7.6,7.6,0,0,1-7.58,7.58H57a7.6,7.6,0,0,1-7.58-7.58v-7.76a47.39,47.39,0,0,1-12.35-4.68L31.87,108a7.62,7.62,0,0,1-10.72,0l-6.31-6.31a7.61,7.61,0,0,1,0-10.72l4.72-4.72A47.38,47.38,0,0,1,14,73.48H7.58A7.6,7.6,0,0,1,0,65.9V57A7.6,7.6,0,0,1,7.58,49.4h6.35a47.2,47.2,0,0,1,5.51-12.94l-4.6-4.59a7.62,7.62,0,0,1,0-10.72l6.31-6.31a7.6,7.6,0,0,1,10.72,0l5,5A46.6,46.6,0,0,1,49.4,15V7.58A7.6,7.6,0,0,1,57,0H65.9a7.6,7.6,0,0,1,7.58,7.58v8.26ZM59.86,36.68a24.6,24.6,0,1,1-24.6,24.59,24.59,24.59,0,0,1,24.6-24.59Z" />
</Canvas>
</Viewbox>
</Button>
</Grid>
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
In Visual Studio(Community Edition, latest version), when I select an image for the background on a page in WPF, the image file is being copied from the images folder to where the page file is. Looked around and can't find a way to turn this off.
Is there a way to turn this off, and why is Visual Studio doing this?
Thanks
[Tim]
|
|
|
|
|
tbenner1960 wrote: when I select an image for the background on a page in WPF
How did you do this, exactly?
tbenner1960 wrote: Is there a way to turn this off, and why is Visual Studio doing this?
I'm wondering how you got VS to do this because it's not a feature that can be turned on and off, it's not even a feature!, and I can't duplicate the problem on my machine.
|
|
|
|
|
Dave
To be more specific, I'm adding the image to the Grid. In the properties window I select a tile brush for the background. The image is in an images folder under the project. What happens then is the selected image is copied from the images folder to the folder where the page is. Also the link to the image points to the copied image, not the file in the images folder.
Yeah strange; these things happen to me.
[Tim]
modified 17-Dec-22 9:45am.
|
|
|
|
|
That explains it. You have MUCH greater control just by typing the XAML directly. Nobody uses the Properties window to do this, or anything else for that matter.
For example:
<DataGrid>
<DataGrid.Background>
<ImageBrush TileMode="Tile"
ImageSource="Images\Tile.bmp"
Viewport="0,0,.2,.4"
ViewportUnits="RelativeToBoundingBox" />
</DataGrid.Background>
</DataGrid>
|
|
|
|
|
I'm trying (or want) to to customize the TabPanel section of the TabControl.
The TabPanel contains the TabItem. (I can customize the TabItem)
I'd like to be able to add a button at the right side of the TabPanel, something like :
|Tab1|Tab2|Tab3 ............... Button|
|-------------------------------------|
| Tab Content |
Is this possible ?
I assume I have to go knee deep in the TabPanel Template or something like that ?
Any suggestions ?
Thanks.
Simple solution:
I found a simple solution, I put the TabControl and the Button on different ZIndex.
It kinda work. maybe will be enough.
CI/CD = Continuous Impediment/Continuous Despair
modified 15-Dec-22 10:47am.
|
|
|
|
|
I'm setting up a sample .Net Core WPF app. I want to implement Unit Testing and I'd like to learn the right way to use DI with Views & View Models.
So far I have this
public partial class App : Application
{
public static IHost? AppHost { get; private set; }
public App()
{
AppHost = Host.CreateDefaultBuilder()
.ConfigureServices((hostContext, services) =>
{
services.AddSingleton<LoginViewModel>();
services.AddSingleton<LoginView>();
})
.Build();
}
protected override void OnStartup(StartupEventArgs e)
{
AppHost!.Start();
base.OnStartup(e);
}
protected override async void OnExit(ExitEventArgs e)
{
await AppHost!.StopAsync();
base.OnExit(e);
}
}
How does the LoginView get the LoginViewModel? In my .Net Framework apps I use a ViewModel locator. Is there a best practice with WPF .Net Core, or would that work fine here also?
Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Watch this: Models - WPF MVVM TUTORIAL #1 - YouTube[^] - he will answer many questions for you, including this one!
Graeme
"I fear not the man who has practiced ten thousand kicks one time, but I fear the man that has practiced one kick ten thousand times!" - Bruce Lee
|
|
|
|
|