|
Hi,
How can I open & run my WPF window from a Windows Form?
I created a WPF application project with a Window named "MyWindow".
I also created a Windows Form named "MyForm" in the same project.
Now, from the App.xaml.cs, I am opening MyForm, where one button is there.
On click of that button, my WPF Window should open.
I used the following code:
<br />
MyWindow w = new MyWindow();<br />
w.Show();<br />
So, now on click of the button, WPF window is opening fine.
But, the problem is, when I am closing my WPF Window, the Form is also closing & exiting the application.
How can I solve this problem? My application should not close until I close my Windows Form. Please help...............
|
|
|
|
|
We really need to see how you've opened the form from App.Xaml before we can comment. Basically, we need to see if the form is running as part of the application message pump.
BTW - this is a really strange architecture. Why are you doing this?
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
It sounds like the WinForm is stealing the events (even though it's not the active window).
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
Set Application.ShutdownMode property to ShutdownMode.OnExplicitShutdown. In WPF, OnMainWindowClose is the default ShutdownMode. The WPF window you open becomes the main window. So closing that window causes the wpf app to shut down unless you explicitly set a different window as main.
|
|
|
|
|
I've noticed a lot of posts on various blogs, etc about this error when referencing image files. Apparently it means that Silverlight can't find your file, or Silverlight doesn't support the format (.gif).
I think like most people experimenting with Silverlight, I haven't formally studied the language, and so I miss many nuances. Here is one where I'm sure it is documented somewhere but I had to learn the hard way.
I had a situation where I was loading an array of image names to be used as a series of ImageBrush elements, so I added the image files to the Properties folder through Blend, and reference them in my code-behind, as...
"Properties/MyImage.png"
however Silverlight didn't recognize them. I had other images referenced the same way in the XAML code and Silverlight had no problem.
It turns out that if you reference an image file (or I assume any other Resource) in the XMAL, Silverlight compiles the Resource into the App.DLL. If they are only referenced in the code-behind this is not the case, so you must include a copy of the Properties folder relative to where the XAP file is run; in this case off my Bin directory.
Hopefully this post will help others avoid my time consuming mistake,
Steve
|
|
|
|
|
If you have static images to embed in your Silverlight app, you can just add
them to your project and set the Build Action property of the image to "Resource".
So, for example, if you add a folder to your project called Images, and add an
image to that folder called Test1.png, you can access it like this:
<!-- From XAML -->
<Image Source="Images/Test1.png" Stretch="None" />
BitmapImage bmp = new BitmapImage(new Uri("yourappmodulename;component/Images/Test1.png", UriKind.Relative));
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Mark,
I appreciate your reply.
Yes my images are marked with the Resource Build Action. Even if I add the yourappmodulename;component/ to the image file string, I still have no luck. With or without yourappmodulename;component/ being added to the string, it appears to create a valid BitmapImage (at least as far as I can see by examining the object in the watch window), but when it comes time to display it, I still get the same error in a Webpage Error messagebox.
If I reference the same image files from XAML, then they are there for the code-behind as well. When I remove the XAML references, the error returns.
The only way I have found to prevent this without adding superfluous references in the XAML, is by including the actual images as I indicated in my original post (although they are actually included relative to Bin/Debug, not Bin; i.e. Bin/Debug/Properties).
I'm not sure what else I am doing or not doing that prevents your solution from working for me.
Thanks again for the response,
Steve
|
|
|
|
|
Just so I'm clear, you want embedded images in your app module, right?
Or are the images external resources?
If you are embedding images in the module:
How are you adding images to your project?
I can't add anything to the "Properties" folder in my projects...
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I'd like them to be embedded, but other than using XAML references, I can't seem to force them to be. I added the image files to the Properties folder through Expression Blend.
Thanks,
Steve
|
|
|
|
|
SBJ wrote: I added the image files to the Properties folder through Expression Blend.
Interesting....that may be the problem.
For project management, I would highly recommend using Visual Studio.
For packaging images with your app, using VS Solution Explorer, you can
1) Create a folder in the project (or use the project's root folder if
you want - messier IMO)
2) Right click the folder, choose Add/Existing Item... and add the images.
By default, the image's build action will be set to Resource - this will embed the
image(s) in the assembly DLL.
At this point you have options -
1) The default (as mentioned above) packages the image(s) in the assembly DLL.
2) You could change the image build type(s) to "Content" and the image(s) will be placed
in the app's XAP file, separate from the DLL.
3) You could change the build type to "None", set the Copy to Output setting to copy always
or copy if newer, and the image(s) will be left on the server -
accessible using a URL. You would use this option for loading on demand
instead of having the image(s) download with the XAP file at startup.
This link shows all these options: Different ways for loading images and files in Silverlight 2 applications[^]
That gives you control over how your images are included with the resulting application
package, so you don't have to guess or rely on some implicit packaging of the images
just because you referenced the image in XAML somewhere
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Actually this project originated in Blend and was first loaded into VS through the Edit in VS menu command in Blend. When I right-click on the Properties folder in VS the only option I have is to Open it. All the other folders include the menu options you would expect. I think you may be right about Blend causing the problem. I can add that to the numerous other problems encountered when developing using the two packages together. Hopefully, VS 2010 and Blend 3 will be more stable!
Thanks again for your help,
Steve
|
|
|
|
|
SBJ wrote: Hopefully, VS 2010 and Blend 3 will be more stable!
Indeed! I was going to say the same thing.
I struggled using the two together at first and found it's best to do project
creation and housekeeping with VS and use Blend just for the stuff it's good at.
At least until they are 100% interchangeable with their project handling...
Also, the latest service packs for both products fixed some of the issues I dealt
with at first, so I'd definitely make sure to keep up-to-date on those
Cheers,
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi there. I'm using WPF and implementing the Model-View-ViewModel pattern. So far, so good.
However, I'm at the point where I want to have the user click a Save button on the view. So I have that Save Click being handled at the ViewModel level. Problem is, it now needs to go through a pre-existing data handling tier. So I can pass the object I have, which is a ViewModel object. But then my data tier has to understand my ViewModel. Or I can split my ViewModel up into discrete data types (int, string, etc.) and pass those through. But I thought I should really have my ViewModel converted back to the Data Model inside the data tier. What's the best approach? I also need to to avoid a circular reference from my main application and my data tier. Help!
Thanks!
|
|
|
|
|
Your model should have no knowledge about your VM. What you need to do is pass the values through to the model from the VM, and you'll update the database from this point. Have a look at my blog entry[^] here to get an idea of what I'm talking about.
|
|
|
|
|
Right. I think the problem that was confusing me is that I have the main ViewModel object (call it PeopleViewModel, if you like) which consists of an ObservableCollection of PersonViewModels as well as a couple of individual PersonViewModel objects of interest. So now I want to save one of these PersonViewModel objects. Where do I convert the PersonViewModel back to a Person? I'm thinking within the PersonViewModel. Then I think you're saying to handle the save at the Person level. Just seems like a lot of things to put my actual data through to get to the save, especially since I then have to go through that pre-existing data handling tier. Am I on the right track, though? Do you know of any code examples? Thanks!
|
|
|
|
|
ml_black wrote: Where do I convert the PersonViewModel back to a Person? I'm thinking within the PersonViewModel.
Correct. In the example link I showed you, the validation in the PersonViewModel is responsible for updating the Person model with the relevant values. What I would tend to do is have a Save command on the View model that called the Save method on your View - yes, it seems like a lot of extra overhead, but it really helps to save your sanity.
Try to think of this as comparable to a n-tiered system, where the UI is the View, the business layer is your ViewModel and the DAL is the model. You wouldn't want the business layer to have to know how to save data to the database, that's the job of the DAL. Similarly, you wouldn't want the DAL to have to be aware of the business rules that surround what is or is not a valid piece of data - that's the job of the BL.
It's this clear separation of concerns that makes MVVM so attractive.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Thanks, Pete. It became more clear once I actually put it into practice!
|
|
|
|
|
I have a wpf application in which i need to create html document dynamically and when I execute my wpf application i have to display the the htmldocument in internet explorer.
for example,in generally we will create table by using table tag in html.
But i want to create those tags using htmldocument class.
if possible,could you please send me source code
Thanks
Rama Krishna
|
|
|
|
|
A simple search would have found you this[^] page.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
I want to alternate the background color for columns in ListViewItem.
Now my ListViewItem.Template looks like this.
<ControlTemplate TargetType="{x:Type ListViewItem}">
<GridViewRowPresenter Content="{TemplateBinding Content}"
Columns="{TemplateBinding GridView.ColumnCollection}"/>
</ControlTemplate>
Now I can't get each column,cause it is sealed by GridViewRowPresenter and i don't konw how to style the appearence of GridViewRowPresenter.
|
|
|
|
|
Take a look at this - Link[^]
|
|
|
|
|
Thanks a lot ABitSmart.I am such a idiot.Thie problem could be easily sovled and I've spend 12 hours figured it out.
There is a demo in MSDN shows How to:Create ListViewItems With a CheckBox.
Well, we could easily replant it for our problem.
<ListViewItem>
<ListViewItem.Resources>
<DataTemplate x:Key="dt1">
<Border Background="Red">
<TextBlock Text="{Binding col1}"/>
</Border>
</DataTemplate>
<DataTemplate x:Key="dt2">
<Border Background="Yellow">
<TextBlock Text="{Binding col2}"/>
</Border>
</DataTemplate>
</ListViewItem.Resources>
<GridView>
<GridViewColumn CellTemplate="{StaticResource dt1}"
Header="Column1 color red"/>
<GridViewColumn CellTemplate="{StaticResource dt2}"
Header="Column2 color yellow"/>
</GridView>
</ListViewItem>
Very simple!
|
|
|
|
|
That's great. It is what was mentioned in the last reply to the post I shared with you.
Have fun
|
|
|
|
|
Alternatively you can use Ivalue Convertor for doing this
Xaml code:
<Window x:Class="AgeConvertor.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:AgeConvertor"
Title="Window2" Height="300" Width="300">
<Window.Resources>
<local:MyConverter x:Key="int2color" />
</Window.Resources>
<Grid>
<ListBox ItemsSource="{Binding MultipleCountryList}" Height="73" VerticalAlignment="Top">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding CountryName}"
Background="{Binding Converter={StaticResource int2color}}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
.CS of Xaml
public partial class Window2 : Window
{
Person p = new Person();
public Window2()
{
InitializeComponent();
this.DataContext = p;
p.LoadCountry();
}
}
ViewModel
public class Person:INotifyPropertyChanged
{
DataSource _objDataSource = new DataSource();
private ObservableCollection<Country> _countrycombo = new ObservableCollection<Country>();
public void LoadCountry()
{
_countrycombo = new ObservableCollection<Country>(_objDataSource.CountryList());
}
public ObservableCollection<Country> MultipleCountryList
{
get
{
return _countrycombo;
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
#region General Propertychange methods
protected void OnPropertyChanged(string PropertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
}
}
#endregion
}
ConvertorClass
public class MyConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
int i = ((Country)value).A;
return i % 2 == 0 ? Brushes.Red : Brushes.Green;
}
public object ConvertBack(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
The CountryEntity class
public class Country
{
public string _countryname = string.Empty;
public string CountryName
{
get
{
return _countryname;
}
set
{
_countryname = value;
}
}
}
The Model/DataSource
public class DataSource
{
ObservableCollection<Country> _country = new ObservableCollection<Country>();
public ObservableCollection<Country> CountryList()
{
_country.Clear();
CountryData();
return _country;
}
private void CountryData()
{
_country.Add(new Country {A=1, CountryName = "India" });
_country.Add(new Country { A = 2, CountryName = "USA" });
_country.Add(new Country { A = 3, CountryName = "Canada" });
_country.Add(new Country { A = 4, CountryName = "Singapore" });
}
}
Niladri Biswas
|
|
|
|
|
Great idea! It's much flexible than my way! I'll try it on in the future!
|
|
|
|