|
You are treating this like a winforms application, STOP. Do some reading on MVVM and data binding if you wish to continue using WPF or actually use winforms.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I got a combo box in wpf form I set the ItemSource to the collection of Dictionary of (Pet Type) and just display the Value and hid the Key
public void BindComboBoxes()
{
this.cboTypes.ItemsSource = new BindingSource(CommonMgr.GetPetTypesDropDown(false), null);
this.cboTypes.DisplayMemberPath = "Value";
this.cboTypes.SelectedValuePath = "Key";
}
Then whenever I type to encode a new Breed Object, and type a text in the cboTypes of something that doesn't exist in its items(not in the db), my program will ask if the end user wants to add that new PetType in the db, if yes, then it will do so.
Then i update the cboTypes using the BindComboBoxes method again, set the cboTypes.Text into the new item and assign the Key to the designated field, but the problem is, it says, it was null. it worked fine in the windows form though. Here's my code:
public Breed GetPageEntity()
{
Breed setEntity = new Breed();
bool doesExist = false;
setEntity.Id = DefaultValue.GetInt(this.txtId.Text);
setEntity.BreedName = DefaultValue.GetString(this.txtName.Text);
try
{
setEntity.PetTypeId = DefaultValue.GetInt(this.cboTypes.SelectedValue.ToString());
}
catch (Exception)
{
var addAnother = MessageBox.Show(String.Format("{0}: This type is not in the database. \nAdd {0} to the database?",
this.cboTypes.Text), "Pet Type Cannot Be Found", MessageBoxButtons.OKCancel);
if (addAnother == System.Windows.Forms.DialogResult.OK)
{
petTypeMgr.Entity = this.PetTypeAdder(cboTypes.Text);
string temp = this.cboTypes.Text;
petTypeMgr.Insert((petTypeMgr.Entity), fUser.Entity.Id, ref doesExist);
BindComboBoxes();
cboTypes.Text = temp;
setEntity.PetTypeId = DefaultValue.GetInt(this.cboTypes.SelectedValue);
}
}
setEntity.Description = DefaultValue.GetString(this.txtDescription.Text);
setEntity.SortOrder = DefaultValue.GetInt(txtSortOrder.Text);
setEntity.StatusId = true;
return setEntity;
}
|
|
|
|
|
Mhister Pamakwas wrote: set the cboTypes.Text into the new item and assign the Key to the designated field
I don't recognise the binding method but your combo does not have a selected record, you do not set the mode to TwoWay and you should not be setting the text but putting the value into the selected record and letting the binding update the UI.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
If is it Possible RichTextBox Document Property Support Binding as FlowDocument.
|
|
|
|
|
Google[^] has plenty of suggestions:
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hello everyone
In my WPF project I have created a UserControl (this is called Applic.xaml), and within this usercontrol I have placed another UserControl (this is called PasswordScreen.xaml). Here is the xaml design code:
<UserControl xmlns:my2="clr-namespace:iPosCloud.com.iposcloud.uiC_.views"
x:Class="iPosCloud.com.iposcloud.main.Applic"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my0="clr-namespace:iPosCloud.com.iposcloud.main"
xmlns:Intersoft="http://intersoft.clientui.com/schemas"
xmlns:my1="clr-namespace:Intersoft.Client.UI.Controls;assembly=Intersoft.Client.UI.Controls"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="800" d:DesignWidth="1024" xmlns:my="http://schemas.microsoft.com/surface/2008">
<UserControl.Background>
<ImageBrush ImageSource="/iPosCloud;component/images/HoneyComb.png" Stretch="Fill" TileMode="Tile" Viewport="0,0,46,25" ViewportUnits="Absolute" />
</UserControl.Background>
<UserControl.Resources>
<DropShadowEffect x:Key="DropShadowEffect" ShadowDepth="0.1" Direction="360" BlurRadius="3" Opacity=".5" />
</UserControl.Resources>
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Name="MainGridApplic">
<Border BorderThickness="2" HorizontalAlignment="Left" Name="border1" VerticalAlignment="Stretch" Margin="4" BorderBrush="{x:Null}">
<Border.BitmapEffect>
<DropShadowBitmapEffect Color="Black" Direction="360" ShadowDepth="10" Opacity=".5" Softness="9" />
</Border.BitmapEffect>
<Intersoft:UXGridPanel Margin="2" Column="1" Row="1" ItemHeight="220" ItemWidth="1190" Name="UXGridPanelLeft">
<Image Name="image1" Source="/iPosCloud;component/images/Logo.png" Width="1000" HorizontalAlignment="Left" />
</Intersoft:UXGridPanel>
</Border>
<Grid Margin="2" HorizontalAlignment="Right">
<my2:PasswordScreen /> "HERE IS THE UserControl THAT I PLACED..."
</Grid>
</Grid>
</UserControl>
In PasswordScreen.xaml there is a button (called Login). On click event when clicked I want to call another xmal file called SwitchboardView.xaml but not firing nothing and the code behind this button is:
private void btnLogin_Click(object sender, RoutedEventArgs e)
{
SwitchboardView sbv = new SwitchboardView();
Applic ap = new Applic();
ap.MainGridApplic.Children.Add(sbv);
}
Could someone please help me solve this problem (what am I doing wrong...)
Kind regards
Lapeci
|
|
|
|
|
For starters you're not modifying the existing Applic UserControl.
You are creating a new one and setting the SwitchboardView into that UserControl.
Then you're dropping them both on the floor!
A positive attitude may not solve every problem, but it will annoy enough people to be worth the effort.
|
|
|
|
|
Hello Matt
Thank you for replying to my question.
I am really struggling on this could you please if you can help me to solve this problem, I'm new to wpf and c# honestly.
Kind regards
Lapeci
|
|
|
|
|
Hello
I am playing around with the async and await keywords using .net4.5 and windows 7.
The problem is that I would like to use RenderTargetBitmap and System.Windows.Media.Imaging.BitmapEncoder to capture a UI element and return a System.Drawing.Bitmap.
The following is some test code. The problem is that it does a lot of work synchronously (e.g. using FileStream and BitmapEnocder).
For this reason, the UI blocks most of the time e.g. the UI is non-responsive.
I was hoping some of the more experienced programmers here could advise me on where I have gone wrong or how best to proceed with this function?
Many thanks for your time....
private async Task<System.Drawing.Bitmap> GetCaptureBitmap(CaptureFrame captureFrame)
{
VisualBrush brush = new VisualBrush(captureFrame);
brush.Stretch = Stretch.Fill;
brush.AlignmentX = AlignmentX.Left;
brush.AlignmentY = AlignmentY.Top;
brush.TileMode = TileMode.None;
Rectangle rect = new Rectangle();
rect.Width = Settings.Instance.Capture.Width;
rect.Height = Settings.Instance.Capture.Height;
rect.Fill = brush;
rect.Measure(new Size(rect.Width, rect.Height));
rect.Arrange(new Rect(0, 0, rect.Width, rect.Height));
System.Windows.Media.Imaging.RenderTargetBitmap bmp = new System.Windows.Media.Imaging.RenderTargetBitmap((int)rect.Width, (int)rect.Height, 96, 96, PixelFormats.Pbgra32);
bmp.Render(rect);
FileStream stream = new FileStream(System.IO.Path.GetTempFileName(), FileMode.Create, FileAccess.ReadWrite, FileShare.Read, 4096, FileOptions.DeleteOnClose);
System.Windows.Media.Imaging.BitmapEncoder encoder = new System.Windows.Media.Imaging.BmpBitmapEncoder();
encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bmp));
encoder.Save(stream);
stream.Flush();
System.Drawing.Bitmap bitmap = (System.Drawing.Bitmap)System.Drawing.Image.FromStream(stream, true, false);
return bitmap;
}
|
|
|
|
|
I did not notice an "await" in your method, so the whole method runs synchronously.
Consider using a BackgroundWorker instead.
The "do work" handler of the BackgroundWorker can do the work in your method; the "completed" event handler can subsequently update the UI, if necessary.
|
|
|
|
|
Hi
Thanks for your advice. I think that you are right. However, this series of functions creates UI controls and then uses rendertargetbitmap to save an image of the control to the disk. I am thinking that I might need to create a UI thread....
I am now going to investigate if it is possible to create a UI thread but some basic searches seems to suggest that it is...
I have used the backgroundworker before but not for UI work. Basically what I want to do is pass some parameters to a thread and for the thread to create the UI controls independently of the main application UI thread.
Hope this makes sense but I am trying to learn this stuff for the first time....I could easily be going about this wrong.
Many thanks....
|
|
|
|
|
If this is a "windows" application, the first / main thread is typically the "UI thread." (You cannot create more than one UI thread).
Besides the "completed" event, which runs on the UI thread, the BackgroundWorker also has a "progress changed" event handler that can be invoked from the "do work" handler via the "on progress changed method"; this "progress changed" handler runs on the UI thread so you can update the UI there.
When using "progress changed" you need to insure that:
- the UI thread is available for "progress changed" to run
- "progress changed" may need to be re-entrant depending on how often / frequently you call it from "do work".
And passing parameters to your threads may not always be practical; you can however "share" memory safely between threads (except UI controls): see "collections.concurrent" and "interlocked" for "non-blocking" possible solutions.
(It's not that something is wrong; some things are just easier than others; and there is usually more than one way).
In general, the BackgroundWorker is suggested if a thread needs to interact with the UI (i.e. via progress changed).
|
|
|
|
|
Technically, you can have more than one UI thread. It's not recommended but it is possible.
|
|
|
|
|
Thanks for that.
Sometimes I equate "shouldn't" with "can't" ...
(Hmmm; now let's see where I can use multiple UI threads in "my" apps).
Yeah, but .... I think we were talking "one window". A window can only have one UI thread.
Multiple windows; one thread for each window; ok.
|
|
|
|
|
Yeah, one window one UI thread.
|
|
|
|
|
Thank you for your help.
I have been researching this and developing some test code.
The thing is that what I am doing is creating a series of bitmaps based upon the state of a user control. The user control displays an image or video and draws some shapes such as lines and rectangles and ellipses. I want to capture the state of the user control several times a second using a visualbrush and use rendertargetbitmap to save to bitmap on the disk.
This is a length process. With lengthy processes, it is recommended to use a background worker. However, I need some way of sharing the visualbrush with the backgroundworker thread. When I search google, people are saying try to refactor the code to use background worker. I am not sure how to do this as I need the background worker to have access to the UI elements. I think I need to use a single-threaded apartment (STA) thread. However, background workers are not STA?
The alternative of creating a new window is possible and using the windows UI thread. However, it is not advised to do lengthy process on a UI thread. I would need to pump for windows messages (I am getting debug warning in visual studio).
So I am not sure what is the best way forward...I might be missing something obvious....
Many thanks....
|
|
|
|
|
I have a rather complex user control for which I use RenderTargetBitmap to create an image that I can "print preview" or "print"; it takes an average of 700ms to render the image (no "saving").
On the other hand, I can do a full screen capture (using System.Drawing.Graphics) AND save the bitmap in about 120ms (where saving is probably the lengthy process).
If I was required to render my user control "several times a second" using RenderTargetBitmap, I would need a multi-core machine dedicated to just that.
Use multiple windows: one for user interaction, and the others for rendering.
|
|
|
|
|
What of the many MVVM frameworks would be recommended for a team that wants to start developing WPF/SL apps?
As a background the team is mainly experienced in VB6/Access VBA, and a lot of TSQL knowledge as well.
Obviously we have a lot to learn, but I'm not sure we want to start on the wrong step and start with the code-behind method.
At the moment I'm leaning toward Prism, but mainly because it seems to have the most complete documentation.
Or will we be taking a step to far, and should stick to code-behind until we have the experience?
|
|
|
|
|
MVVM Light now comes with training from Pluralsight. It's a good place to start.
|
|
|
|
|
I saw that, have you done the course? MVVMLight looked fairly good, enough to do it, without too much to digest. The VS snippets and templates where a nice help too.
|
|
|
|
|
I haven't done the course - primarily because I know MVVM Light pretty much inside out, and Laurent is using some of my codebase in there already. That reminds me, I really must poke him to update the attribution list to point back to my entries.
|
|
|
|
|
Have you heard about:
Catel
|
|
|
|
|
Thanks, looking at it now, certainly seems to be aimed at the type of apps we'll be creating.
I like the emphasis on documentation too.
|
|
|
|
|
I'm another vote for MVVM Lite from Galasoft, we were lucky enough to start with that before most of the others appeared.
You do know the Silverlight is dead, MS are no longer extending it and it has and EOL in 2022 or thereabouts.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I am worried about Silverlight, but has there actually been an official announcement of its EOL? I was leaning towards Silverlight as it can do desktop client and also be easily web hosted too.
Given that most of our apps are Access 97 and VB6, and were still on XP...with Win7 maybe appearing next year. The Win Store apps are a no go, and I've not seen any actual Win Store apps that aren't glorified web pages.
|
|
|
|