Reputation: 263
I'm trying to create an UserControl for a UWP Project where I want to have an indicator of the unit the user is working with.
To achieve that I've created an enum and defined the types of units the control will handle as in the following example:
public static class UnitEnum
{
public enum Unit
{
Size,
Weight,
Currency,
Percentage
}
}
Then I've created the property ValueUnit
on my UserControl which should be set when adding that control to the UI as following:
<usercontrols:NumericBox ValueUnit="Currency"/>
<usercontrols:NumericBox ValueUnit="Percentage"/>
<usercontrols:NumericBox ValueUnit="Size"/>
<usercontrols:NumericBox ValueUnit="Weight"/>
Inside the UserContol I have a switch case where the indicator string changes to match the system default unit for that data type (Ex: Currency = €, Size = M (from meters)) as following:
switch (ValueUnit)
{
case Unit.Currency:
UnitSymbol = System.Globalization.NumberFormatInfo.CurrentInfo.CurrencySymbol;
break;
case Unit.Percentage:
UnitSymbol = "%";
break;
case Unit.Size:
if (System.Globalization.RegionInfo.CurrentRegion.IsMetric) UnitSymbol = "m";
break;
case Unit.Weight:
if (System.Globalization.RegionInfo.CurrentRegion.IsMetric) UnitSymbol = "Kg";
break;
}
Then I Bind the UnitSymbol
to the correspondent TextBlock:
<TextBlock Text="{x:Bind UnitSymbol}" VerticalAlignment="Center" Margin="5"/>
But when running it always shows me the symbol that corresponds to the first value of the enum (in this case size), even when I've set the ValueUnit
to any other value in the XAML.
Example:
Update 1
I've added a name to the UserControl
with ValueUnit == Currency
as shown here:
<usercontrols:NumericBox ValueUnit="Currency" x:Name="CurrencyBox"/>
And in the code-behind I've added the following line and placed a breakpoint:
var a = CurrencyBox.ValueUnit;
When the app stops on that line a = Size
, CurrencyBox.ValueUnit = Currency
, both derived from Enums.UnitEnum.Unit
, there when advancing the execution a
changes to Currency
too but in the end it still shows me the size symbol (m).
Update 2
After some testing I've found that any of the other properties in the UserControl
aren't working, just the ones that get Strings
.
Example:
<usercontrols:NumericBox Header="Teste" ValueUnit="Currency" HasCalc="True" x:Name="CurrencyBox"/>
Header
(that's is a string
) is working. ✅ValueUnit
(that's a Unit
from my UnitEnum
) isn't working ❌HasCalc
(that's a normal bool
) isn't working ❌Props in the UserControl code-behind:
public string Header { get; set; }
public Unit ValueUnit { get; set; }
public bool HasCalc { get; set; }
Also, the property UnitSymbol
(string
) is working since the value in it is shown.
Upvotes: 0
Views: 100
Reputation: 8681
I could reproduce your issue using the code you posted. The problem might be related to DataBinding
. The default binding mode of X:bind is onetime which means it won't change once the binding is created.
The main thing I changed is to use Binding instead of X:Bind.
For example in UserControl:
<StackPanel>
<TextBlock x:Name="MyTextBlock" Text="{Binding UnitSymbol,Mode=OneWay}"/>
<Button Content="{Binding Header,Mode=OneWay}" Visibility="{Binding HasCalc,Mode=OneWay}"/>
</StackPanel>
And in the code-behind I have:
public TestUserControl()
{
this.InitializeComponent();
this.Loaded += TestUserControl_Loaded;
}
private void TestUserControl_Loaded(object sender, RoutedEventArgs e)
{
switch (ValueUnit)
{
case UnitEnum.Unit.Currency:
UnitSymbol = System.Globalization.NumberFormatInfo.CurrentInfo.CurrencySymbol;
break;
case UnitEnum.Unit.Percentage:
UnitSymbol = "%";
break;
case UnitEnum.Unit.Size:
if (System.Globalization.RegionInfo.CurrentRegion.IsMetric) UnitSymbol = "m";
break;
case UnitEnum.Unit.Weight:
if (System.Globalization.RegionInfo.CurrentRegion.IsMetric) UnitSymbol = "Kg";
break;
}
this.DataContext = this;
}
Then I'm calling it from the MainPage like this:
<local:TestUserControl HasCalc="False" ValueUnit="Percentage" Header="Yes"/>
And the result looks like this:
Since it's a very simple test, if you still have questions, please let me know.
Upvotes: 1
Reputation: 370
ValueUnit="Currency"
sets ValueUnit to the string "Currency" and not to a value in UnitEnum
. Then, when you are in your switch statement, nothing matches and the default value of 0 is returned.
To get around this, you can change your case
conditions to use the .ToString()
method, like this:
case Unit.Currency.ToString():
UnitSymbol = System.Globalization.NumberFormatInfo.CurrentInfo.CurrencySymbol;
break;
Upvotes: 2