|
Apologies for the shouting but this is important.
When answering a question please:
- Read the question carefully
- Understand that English isn't everyone's first language so be lenient of bad spelling and grammar
- If a question is poorly phrased then either ask for clarification, ignore it, or mark it down. Insults are not welcome
- If the question is inappropriate then click the 'vote to remove message' button
Insults, slap-downs and sarcasm aren't welcome. Let's work to help developers, not make them feel stupid.
cheers,
Chris Maunder
The Code Project Co-founder
Microsoft C++ MVP
|
|
|
|
|
For those new to message boards please try to follow a few simple rules when posting your question.- Choose the correct forum for your message. Posting a VB.NET question in the C++ forum will end in tears.
- Be specific! Don't ask "can someone send me the code to create an application that does 'X'. Pinpoint exactly what it is you need help with.
- Keep the subject line brief, but descriptive. eg "File Serialization problem"
- Keep the question as brief as possible. If you have to include code, include the smallest snippet of code you can.
- Be careful when including code that you haven't made a typo. Typing mistakes can become the focal point instead of the actual question you asked.
- Do not remove or empty a message if others have replied. Keep the thread intact and available for others to search and read. If your problem was answered then edit your message and add "[Solved]" to the subject line of the original post, and cast an approval vote to the one or several answers that really helped you.
- If you are posting source code with your question, place it inside <pre></pre> tags. We advise you also check the "Encode "<" (and other HTML) characters when pasting" checkbox before pasting anything inside the PRE block, and make sure "Use HTML in this post" check box is checked.
- Be courteous and DON'T SHOUT. Everyone here helps because they enjoy helping others, not because it's their job.
- Please do not post links to your question into an unrelated forum such as the lounge. It will be deleted. Likewise, do not post the same question in more than one forum.
- Do not be abusive, offensive, inappropriate or harass anyone on the boards. Doing so will get you kicked off and banned. Play nice.
- If you have a school or university assignment, assume that your teacher or lecturer is also reading these forums.
- No advertising or soliciting.
- We reserve the right to move your posts to a more appropriate forum or to delete anything deemed inappropriate or illegal.
cheers,
Chris Maunder
The Code Project Co-founder
Microsoft C++ MVP
|
|
|
|
|
I wanna know isn't save in my computer .If i run my game and score high score and then close my game.When i run it again next time there will remain my high score every time i run.
Plz tell me and thank you.
|
|
|
|
|
|
I have a textbox display that has incoming data from a serial port that I display. After 200 lines of data that has came in I want to remove 20 lines at the beginning. Here is my code:
private void TextBoxADCPDisplay_TextChanged(object sender, TextChangedEventArgs e)
{
TextBoxADCPDisplay.SelectionStart = TextBoxADCPDisplay.Text.Length;
TextBoxADCPDisplay.ScrollToEnd();
if (TextBoxADCPDisplay.LineCount > 200)
{
TextBoxADCPDisplay.Select(0, 20);
TextBoxADCPDisplay.SelectedText = "";
}
}
I get some strange behavior. Where after I hit 200 lines I see multiple lines that are the same where they should be different, but if I manually scroll up a little and scroll back down the lines of data that are coming in display as they should. What am I doing wrong?
|
|
|
|
|
geomeo123 wrote:
TextBoxADCPDisplay.Select(0, 20);
TextBoxADCPDisplay.SelectedText = ""; Well, start with the fact that the code you've written will not remove 20 lines; it will remove 20 characters.
Parameters
start The zero-based character index of the first character in the selection.
length The length of the selection, in characters.
So unless you can guarantee that every line is precisely 1 character long, and that the Select method ignores the line-break characters, your code isn't going to do what you want it to do.
If you want to remove 20 lines, you'll need to call TextBox.GetLineLength[^] for each line you want to remove, and sum up the line lengths to get the number of characters you need to select. NB: You'll need to test whether or not the returned value includes the line-break characters, as that's not clear from the documentation.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I had this code for my old windows form. At the time I didn't pay much attention to it as I had found it on a forum somewhere and I just copied and pasted. The original problem was in Windows forms. After a certain period of time of taking data in the entire program would crash. The code above resolved it, but moving over to WPF I'm finding a lot of things don't work as they should or even have the same methods. I am liking the fact that WPF is a lot more forgiving on certain things though so all good.
Thanks
|
|
|
|
|
I'm looking at the deault combobox style[^]
I'm not entirely sure how to ask what I'm thinking, so please bear with me...
Is it possible to replace a part of the control? For example, if I wanted to create a custome combobox. I know in the C# file I just subclass it
public class MyComboBox : ComboBox
{
static MyComboBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyComboBox),
new FrameworkPropertyMetadata(typeof(MyComboBox)));
}
}
But in the Generic.xaml, I seems like I would need to have ALL the default xaml from the site above - so that it looks & functions like a combobox.
Is there a way for me to provide just a new ContentSite? Can I justy define that in my Generic.xaml and somehow apply that to my combobox?
Ot do I need to copy in all the default XAML?
Thanks
In theory, theory and practice are the same. But in practice, they never are.”
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
|
|
|
|
|
I seriously recommend picking up a book on WPF. This kind of stuff is covered in them.
Yes, you can replace just about anything you want in a WPF control. You just have to provide the implementation of what you want to replace, not the entire control or any of the implementation above what you're replacing.
The trick is knowing what in the XAML tree you need to replace, and where in the tree you need to provide it. Say you want to replace the Text property of a TextBox with a set of async priority bindings. You don't need to provide all the other XAML for a TextBox. You just need to specify the bindings:
<TextBox>
<TextBox.Text>
<PriorityBinding>
<Binding Path="SlowProperty" IsAsync="true" />
<Binding Path="MediumProperty" IsAsync="true" />
<Binding Path="FastProperty" />
</PriorityBinding>
</TextBox.Text>
</TextBox>
There isn't a XAML and C# component to a control. XAML is run through a compiler to generate the C# code. They are interchangeable, not complimentary to each other. The C# code for the control can exist entirely without XAML and the control will still work just fine. XAML is just a structured representation of a bunch of C# classes with properties being set.
Can you replace the ContentControl of a ComboBox? Sure. Can you replace the XAML and provide your own render code for the Combo? Again, sure.
Is it appropriate to do this? I have no idea what you're trying to do with the Combo, so I can't say.
It's also possible to build your own control that looks and acts like a Combo, but doesn't use a ComboBox, or is even based on the existing ComboBox, control at all. You can build one using a TextBox and a PopUp control to do weird stuff like a "ComboBox" that drops down and shows a bunch of TreeView controls in it.
modified 15-Jun-24 12:16pm.
|
|
|
|
|
I do WPF for a living. I have the Control Development book. In the part about creating custom controls, ALL the xaml from the template is used in the examples.
I want to create subclass of a combox box. I don't need the XAML for the button or the textbox, or any other part except for the content site. If you go look at the XAML in the link I provided, I want to replace this:
<ContentPresenter x:Name="ContentSite"
IsHitTestVisible="False"
Content="{TemplateBinding SelectionBoxItem}"
ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
Margin="3,3,23,3"
VerticalAlignment="Stretch"
HorizontalAlignment="Left">
Something like this
public class MyComboBox : ComboBox
{
static MyComboBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyComboBox),
new FrameworkPropertyMetadata(typeof(MyComboBox)));
}
}
and
<Style TargetType="{x:Type ctrls:MyComboBox}">
<pre>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBox">
</ControlTemplate>
</Setter.Value>
</Setter>
If I do this XAML, the ENTIRE template is replaced, so I'd have to recreate the textbox, button, etc.
What I'm asking is, instead of replacing the ENTIRE TEMPLATE, can I somehow replace just a small piece of that template?
In theory, theory and practice are the same. But in practice, they never are.”
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
|
|
|
|
|
I have some code I'm trying to move to my view model. And it appears that my Invoke method does not work in it. I have the following code...
public delegate void SetTextCallBackSMT(string text);
Handing my serial port
string SMTData = SpSMT.ReadExisting();
if (SMTData.Contains("\r"))
{
modeSMT = ModeSMT.readLineSMTData;
}
else
{
Invoke(new SetTextCallBackSMT(SetTextSMTDisplay), SMTData.ToString());
}
And my set text method....
private void SetTextSMTDisplay(string text)
{
this.RichTextBoxSMTDisplay.Text += text;
}
The problem I'm having is that Invoke does not exist in the current context. I've tried adding forms name space. I've tried Dispatcher.Invoke, but I can't get that to work either. Any ideas?
|
|
|
|
|
Most code in the view-model doesn't need to worry about threading issues. The binding to the view will usually take care of that for you.
The only exception I'm aware of is if you're updating a collection from a background thread, where you may need to call the BindingOperations.EnableCollectionSynchronization[^] method to get it to work properly.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
This is extremely helpful to know! Thanks!
|
|
|
|
|
I'm trying to update my program to WPF MVVM with Icommands to make my program look a little better and reduce the amount of event handlers, clutter etc, which I've managed to make workish. Well if I press a button in WPF my message box executes, so that's always good right? But I have some properties in my old windows form load event that I need to move over and I'm not exactly sure how I would implement that. I'm using the serialport io namespace and so I'm loading things like this at form load time:
SP.Handshake = Handshake.None;
SP.DataReceived += new SerialDataReceivedEventHandler(SP_DataReceived);
SP meaning serial port. Do I put the above in my wpf form load event and make these public so that my modelview class can see them? Or is there an alternative? My model view class inherits from my viewbasemodel so it doesn't allow inheriting from another base class. I could move them to my viewbasemodel class, but I'm not sure about that one either. I still have to make them run at form load. I have to say I'm only half understanding the modelview at the moment. The tutorials online aren't clicking just yet. But I have made it workish so that's always good.
|
|
|
|
|
geomeo123 wrote: Do I put the above in my wpf form load event and make these public so that my modelview class can see them?
No. The ViewModel should not have any reference to the view.
The quick fix would be to move the serial port comms into the ViewModel.
A better option would probably be to move it to a separate "service" class consumed by the ViewModel, particularly if you want to introduce unit tests for your ViewModel classes.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Ok so I put them in ViewModel as a quick fix for now, but how would I call that from my xaml form? The xaml loaded event doesn't allow me to bind to my static resource(thinking I could just do another Icommand). Maybe because my static resource is called after I'm putting in loaded in xaml, but I can't think of any other way.
|
|
|
|
|
The typical solution would be to use an "attached behaviour" to bind the Loaded event to your command.
Introduction to Attached Behaviors in WPF[^]
For example:
public static class LoadedBehavior
{
public static readonly DependencyProperty LoadedCommandProperty = DependencyProperty.RegisterAttached(
"LoadedCommand",
typeof(ICommand),
typeof(LoadedBehavior),
new PropertyMetadata(null, OnLoadedCommandChanged));
public static ICommand GetLoadedCommand(FrameworkElement obj)
{
return (ICommand)obj.GetValue(LoadedCommandProperty);
}
public static void SetLoadedCommand(FrameworkElement obj, ICommand value)
{
obj.SetValue(LoadedCommandProperty, value);
}
private static void OnLoadedCommandChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
FrameworkElement element = obj as FrameworkElement;
if (element == null) return;
if (e.OldValue == null)
{
element.Loaded += OnLoaded;
}
else if (e.NewValue == null)
{
element.Loaded -= OnLoaded;
}
}
public static readonly DependencyProperty LoadedCommandParameterProperty = DependencyProperty.RegisterAttached(
"LoadedCommandParameter",
typeof(object),
typeof(LoadedBehavior),
new PropertyMetadata(null));
public static object GetLoadedCommandParameter(FrameworkElement obj)
{
return obj.GetValue(LoadedCommandParameterProperty);
}
public static void SetLoadedCommandParameter(FrameworkElement obj, object value)
{
obj.SetValue(LoadedCommandParameterProperty, value);
}
private static void OnLoaded(object sender, RoutedEventArgs e)
{
FrameworkElement element = sender as FrameworkElement;
if (element == null) return;
ICommand command = GetLoadedCommand(element);
if (command == null) return;
object parameter = GetLoadedCommandParameter(element);
command.Execute(parameter);
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
|
Thank you much Richard. I think I got it. Thanks again!
|
|
|
|
|
I'm trying to use a WrapPanel inside an ItemsControl. I'm following this[^].
The difference is that mine is bound to a list of objects called Backups.
Here's what I have. When I run it, I don't see any of the data.
<Border Grid.Row="1"
CornerRadius="20"
Margin="35"
Padding="10"
Background="Tan"
BorderThickness="3"
BorderBrush="SteelBlue">
<ScrollViewer VerticalScrollBarVisibility="Auto"
Background="Teal"
Margin="10">
<ItemsControl ItemsSource="{Binding Backups}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Background="Salmon"
BorderBrush="Blue"
BorderThickness="3">
<StackPanel Orientation="Vertical">
<Rectangle Margin="5"
Width="100"
Height="100"
Fill="Yellow"
HorizontalAlignment="Center"/>
<TextBlock Text="THIS IS A TEST"
HorizontalAlignment="Center"
Margin="5"/>
</StackPanel>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</ScrollViewer>
</Border>
In theory, theory and practice are the same. But in practice, they never are.”
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
modified 29-May-24 22:23pm.
|
|
|
|
|
If you remove the ItemsPanel , do you see the items?
If not, are there any binding errors logged in the Visual Studio output window?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I removed the ItemsPanel, and there are no binding errors, yet I still see nothing.
Also, I just created a quick test app doing the same thing, and it works:
GitHub - MaroisConsulting/Marois.WrapPanelDemo
In theory, theory and practice are the same. But in practice, they never are.”
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
|
|
|
|
|
Kevin Marois wrote: I removed the ItemsPanel, and there are no binding errors, yet I still see nothing.
Which means the problem is nothing to do with the items panel; either it's not binding to the collection, or the collection is empty.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I am facing an exception while WebView initialize. It is working perfectly fine when i debug my code on VS 2019 but when i make an installer and install it on VM Windows 10 Machine it gives me an exception which i can get with help of Logs.
I have added UDF as well in my client machine and install Microsoft edge runtime(124.0.2478.97)
Please Suggest any one what i need to add to resolve this issue.
Code Block
MainWindow.xaml.cs
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.Wpf;
public MainWindow()
{
InitializeComponent();
InitializeAndNavigateAsync( url);
}
public async Task InitializeAndNavigateAsync(string url)
{
await InitializeWebViewAsync();
await NavigateToUrlAsync(url);
}
private async Task InitializeWebViewAsync()
{
try
{
log.Debug($"WebView2 initialize start");
await webView.EnsureCoreWebView2Async(null);
log.Debug($"WebView2 initialize stop");
}
catch (Exception e)
{
log.Debug($"WebView2 initialization failed.{e.Message}");
log.Debug($"WebView2 initialization failed.{e.StackTrace}");
}
}
private async Task NavigateToUrlAsync(string url)
{
try
{
log.Debug($"WebView2 NavigateToUrlAsync start");
webView.CoreWebView2.Navigate(url);
log.Debug($"WebView2 NavigateToUrlAsync stop");
}
catch (Exception e)
{
log.Debug($"WebView2 NavigateToUrlAsync failed.{e.Message}");
log.Debug($"WebView2 NavigateToUrlAsync failed.{e.StackTrace}");
}
}
Exception is :
WebView2 initialization failed.Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE))
2024-05-14 06:34:45.4923|DEBUG|debug|WebView2 initialization failed. at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
at Microsoft.Web.WebView2.Core.CoreWebView2Environment.<CreateCoreWebView2ControllerAsync>d__80.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Web.WebView2.Wpf.WebView2.<>c__DisplayClass32_0.<<EnsureCoreWebView2Async>g__Init|0>d.MoveNext()
2024-05-14 06:34:45.4923|DEBUG|debug|WebView2 initialization failed.mscorlib
|
|
|
|
|
In your InitializeWebViewAsync method, try creating a CoreWebView2Environment object (you'll need to give it a folder path to use) and passing that to the EnsureCoreWebView2Async method.
|
|
|
|