|
I have a construction management app that is used to manage house framing. The client now want to use it to manage finish work, such as doors and cabinets, etc. The structure of the data is very similar - almost identical, and functionally the app would work the same with a few one-off views for each. My thought is that when the user logs in, they would now also pick which company they want to work in.
The question is this... There are going to be some TextBlocks that will need to be changed depending on what company the user is in. For example, the word "Project" might need to say "Task" depending on the company.
One idea I'm tossing around is to store these words in a resources file, something like:
<data name="a_project_caption" xml:space="preserve">
<value>Project</value>
</data>
<data name="b_project_caption" xml:space="preserve">
<value>Task</value>
</data>
Next, in the XAML I would have something like a behavior maybe?:
<TextBlock Tag="project_caption" beh:ResolveCaption/>
The behavior takes the value in the Tag and sets the textblock's text based on what it finds in the resource file, and throws if it can't resolve it.
I'm open to suggestions on this.
Thanks!
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
modified 14-May-21 16:58pm.
|
|
|
|
|
I know absolutely zilch about your environment, but your question reminds me of something else which should already have been solved by someone else.
I18N - Internationalisation.
Company 1 <=> English
Company 2 <=> French
...
Cheers,
Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
Not really. I've done internalization, and in that case you create a resource file for each language, then ship the resource file you need.
This has nothing to do with language. In this case, there is will only be one resource file, and the captions for the textblocks are ALL in it, and it's only the selection of the company that decides which gets used.
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
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
|
|
|
|
|