|
Do something like this?
InputLanguageManager.SetInputLanguage(myTextBox, CultureInfo.CreateSpecificCulture("fr"));
Looks like you can do the entire window too, by passing this instead of the textbox.
".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
modified 12-Sep-24 10:28am.
|
|
|
|
|
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.
|
|
|
|
|
Did that 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
|
|
|
|
|
I have a textblock title in very large font, we'll call it titleTextBlock for simplicity. I can't seem to figure out how to bind it to my tabcontrol tabitem textblocks so that when a user clicks on the tabitem it shows what they've clicked on in titleTextBlock. When I try binding to element I can only seem to bind to one tabitem element. Any ideas?
|
|
|
|
|
Hello, in WPF I want to use a combobox powered by Richtextbox (or something allowing me to do the same). Richtextbox contains text and images on the same line. I would like when we click on the comboxbox that we can see each item with the text + images, and when we select an element of the Combobox, it appears as a selected element and appears correctly with the text and the images. Is it possible (I guess yes) in this case, how to do it using the MVVM pattern? THANKS
|
|
|
|
|
I have a flat text file of which here is an example line:
sample 1 : {Image1} blah {image2} blah.
sample 2 : blah {Image1} blah
sample 3 : blah blah
sample 4 : {Image1} blah
(each line may be different)
With Image1 and Image2 which represents the name of the image file in the Images of the project.
|
|
|
|
|
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
|
|
|
|