|
I'd store these values in something like a database and populate it accordingly.
|
|
|
|
|
Whenever I needed to do this I always used the control name, not the tag - it is so useful for other things. The data was always stored in a table possibly with other customisation of each form. You know they are going to vary more than labels between entities.
For major variations I would create a new form and deal with it in the navigation.
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
In the Loaded event of the Label, I'd look up the current caption in the app's current "translation" dictionary, and substitute whatever words were found.
I've been transliterating that way: text with "old" words that get substituted with new words for given author. I sometimes hop twice and do a final IPA (pronunciation) translation.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
Here's an example of handling data beign pasted into a textbox:
Masking input to a WPF TextBox | Atomic Blography
In the XAML he wires up the Pasting hander like this:
<TextBox PreviewTextInput="PreviewTextInputHandler"
DataObject.Pasting="PastingHandler" />
I would like to handle all of this in a TextBox override class. I can easily bring in the preview handler part, but how would you wire up the DataObject.Pasting="PastingHandler" from within the base class? His code is doing in the XAML where the TextBox is defined.
I want to encapsulate all of this in my TextBox class, but there's no override, and the DataObject.Pasting event is set to read only so you can't hook to the event.
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
|
That did it. Thanks!
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
I have a nullable decimal TaxRate property on an entity. I want to format/style the textbox to enter a percentage. What I have so far doesn't work. If I start out by entering a decimal point, then it works fine.
If I start out by entering a digit, then I cannot enter a decimal.
<Style x:Key="percentageStyle"
TargetType="{x:Type TextBox}"
BasedOn="{StaticResource textBoxStyle}">
<pre>
<Setter Property="Text" Value="{Binding SelectedStatesCountiesCities.TaxRate, StringFormat={}{0:N2}%}" />
<Style.Triggers>
<Trigger Property="IsKeyboardFocused" Value="True">
<Setter Property="Text" Value="{Binding SelectedStatesCountiesCities.TaxRate}" />
</Trigger>
</Style.Triggers>
<TextBox Grid.Row="1"
Grid.Column="2"
Text="{Binding SelectedStatesCountiesCities.TaxRate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Style="{StaticResource percentageStyle}"
HorizontalAlignment="Left"
IsEnabled="True"
Width="100"/>
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
The UpdateSourceTrigger=PropertyChanged option means that your source property is updated every time you press a key in the TextBox .
When you type a . character, one of two things will happen:
- The box is currently empty.
"." on its own cannot be parsed as a decimal , so the property will not be updated. - The box contains an integer:
decimal.Parse("42.") returns 42 .- The property is updated to
42 , triggering the PropertyChanged event. - The binding on the box updates to reflect the new property value - the text is set to
"42" .
Depending on what you are trying to do, there may be ways around this - for example, manually updating the source value when the user presses Enter:
c# - Binding to a double with StringFormat on a TextBox - Stack Overflow[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
|
Does anyone have (or know of) a xshd file for XAML files in AvalonEdit?
I can't find one on google to save my my soul...
".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 ComboBox bound to a list of objects. When the user selects one, I want to warn the user. If they select No to my warning, I want to prevent the change.
The code in the VM's property setter runs, and it even appears to set SelectedVendor back to what it was, but in the UI it doesn't revert.
XAML
<ComboBox Grid.Row="0"
Grid.Column="1"
ItemsSource="{Binding JobVendors, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding SelectedVendor, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Style="{StaticResource localComboStyle}"/>
ViewModel
private CompanyHeaderEntity _SelectedVendor;
public CompanyHeaderEntity SelectedVendor
{
get { return _SelectedVendor; }
set
{
var currentVendor = _SelectedVendor;
if (_SelectedVendor != value)
{
bool okToChange = true;
if (!_isLoading &&
PurchaseOrderHeader.PurchaseOrderItems != null &&
PurchaseOrderHeader.PurchaseOrderItems.Count > 0)
{
var message = "Changing vendors will remove all selected items. Continue?";
okToChange = MessageBox.Show(message, "Change Vendor", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes;
if (okToChange)
{
PurchaseOrderHeader.PurchaseOrderItems.Clear();
}
}
if (okToChange)
{
_SelectedVendor = value;
RaisePropertyChanged("SelectedVendor");
VendorSelected();
}
else
{
if (currentVendor != null)
{
_SelectedVendor = currentVendor;
}
}
}
}
}
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
modified 5-Apr-21 14:37pm.
|
|
|
|
|
In your last if statement, try adding this after setting the field to the current vendor:
RaisePropertyChanged("SelectedVendor");
".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
|
|
|
|
|
VS 2017
.Net 4.72
-------------------------------
I'm writing a WPF app that instantiates a class like this:
public class MyClass : INotifyPropertyChanged
{
private string currentFile;
private int fileCount ;
public string CurrentFile
{
get {return this.currentFile;}
set {if(value!=this.currentFile){this.currentFile=value;this.NtfyPropChange();}}
}
public int FileCount
{
get {return this.fileCount;}
set {if (value != this.fileCount){this.fileCount=value;this.NtfyPropChange();}}
}
public MyClass()
{
this.CurrentFile = string.Empty;
this.FileCount = 0;
}
public void DoSomethingWithFiles()
{
string[] files = Directory,.GetFiles(@"c:\mypath", *.*);
foreach(string file in files)
{
this.CurrentFile = file;
this.FileCount += 1;
}
}
}
In the XAML, I'm binding like this:
<TextBlock Text="{Binding x:Name="textblockCurrentFile" Path=myObject.CurrentFile}"/>
<TextBlock Text="{Binding x:Name="textblockFileCount" Path=myObject.FileCount}"/>
In my INotifyPropertyChanged -derived form, I have set the DataContext , instantiated MyObject , and when I inspect everything under the debugger, the two MyObject properties in question are being updated as expected as the method loop proceeds. In fact, if I hook the MyObject.PropertyChanged event in the form code, it is indeed called for every property change event.
Problem:
The form controls that are bound to these properties don't update when the property values change.
The MyObject class is created on the UI thread, and the method is not, itself, being executed in a different thread.
What am I dong wrong (or not doing at all)?
I tried doing this in the PropertyChanged event handler in the form, but it didn't fix my problem:
private void Tfs_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
Dispatcher.BeginInvoke(new Action(() =>
{
this.textblockCurrentFile.Text = this.myObject.CurrentFile;
this.textblockFileCount.Text = this.myObject.FileCount.ToString();
}));
}
".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
|
|
|
|
|
Are there any binding errors showing in the output window? If you use the live tree window, what do you see?
|
|
|
|
|
That was the first thing I looked for - no binding exceptions at all.
The form is actually a wizard page, and if I go to prev or next page and then come back, the text blocks show that last value to which they were set, so that tells me binding is okay.
New info - if an exception happens and the code shows a message box, the text blocks update.
It's almost like it's too busy to actually hit the UI, but like I said, this app isn't using threads.
".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
|
|
|
|
|
At a guess, do the bound values magically update when the loop finishes?
If you're calling DoSomethingWithFiles on the UI thread, you're blocking the UI thread until the method returns. No property updates will have any effect on the UI.
Either move the method to a background thread, or make it async and yield on every loop iteration:
public void DoSomethingWithFiles()
{
_ = DoSomethingWithFilesAsync();
}
private async Task DoSomethingWithFilesAsync()
{
string[] files = Directory.EnumerateFiles(@"c:\mypath", "*.*");
foreach (string file in files)
{
this.CurrentFile = file;
this.FileCount += 1;
await Task.Yield();
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Well, that didn't work either. So I moved the code from the object into the form itself,
and it STILL doesn't update the controls. :/
It only updates the ui if i DON'T execute the code...
".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 29-Mar-21 9:12am.
|
|
|
|
|
What's in the Window's code behind?
I'd (normally) expect to see:
public MyClass myObject {get;} = new MyClass();
...
(In the constructor; or preferably in the Loaded event - i.e. "after" all other initializations)
this.DataContext = this;
As for:
this.textblockCurrentFile.Text = this.myObject.CurrentFile;
You're referencing the "binding"; you should be referencing (the name of) the associated TextBlock, in this case.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
In the constructor:
this.DataContext = this; also, the object is declared in the form as follows:
private MyObject myObj;
public MyObject MyObj { get { return this.myObj; } set { this.myObj = value; this.NotifyPropertyChange(); } } I already showed the xaml in the original post.
There are no binding exceptions being thrown when the code executes.
I have verified that the properties that are bound to the textblocks are indeed being updated (the setter in the properties is being called, and the values are in fact being updated), and that the form itself is recieving the PropertyChanged events (I hooked the PropertyChanged event to make sure).
As stated before, I am running this on the same thread as the UI, so cross-thread UI issues should not (and don't appear to) be a factor since the properties are getting updated and the form is being notified, and there are no exceptions being raised.
I even tried bringing the code into the form from the object, and the UI still doesn't update until the method that's running the code exits.
".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
|
|
|
|
|
Binding requires a public property:
Path=myObject.CurrentFile
...
private MyObject myObj;
public MyObject MyObj { get { return this.myObj; } set { this.myObj = value; this.NotifyPropertyChange(); } }
Don't see any matches.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
Look again. The field is private the property is public.
".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
|
|
|
|
|
The "name" of your public property is "MyObj".
The "object" you're binding to is "myObject" (which is neither a property or field).
It does not compute.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
I'm typing the code manually, because I can't just copy paste snippets due to security-imposed domain boundaries. I hate typing, so sometimes I might use shortcuts.
The long and short of it is that I've verified (through the debugger) that the bound properties are indeed being updated during the "long process", and the form is being notified of those changes as they occur, but the UI is not showing the changed values until the method either throws an exception or exits entirely.
".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
|
|
|
|
|
#realJSOP wrote:
this.DataContext = this; It could be a conflict with binding change notifications to INPC properties on a DependencyObject.
Have you tried using a DependencyProperty instead?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|