Show component with changed property in design (not default) - c#

I have a little problem with my component. This looks like my property:
private ViewType _viewType = ViewType.Week;
public ViewType DisplayType
{
get { return _viewType; }
set
{
_viewType = value;
if (panelKalendar != null)
panelKalendar.Invalidate();
}
}
and this I have in Kalendar_Load():
...
if (this._viewType == ViewType.Month)
panelKalendar.Top = yPoloha;
else if (this._viewType == ViewType.Week)
panelKalendar.Top = yPoloha + VYSKA_BUNKY;
...
(class ViewType)
public enum ViewType
{
Week,
Month,
}
when I add my component to app It looks fine. I have default week, my component is in design draw with week looks. But when I change it to Month, in Design it is shown bad, it gets the second if in kalendar_load (panelKalendar.Top = yPoloha + VYSKA_BUNKY). When I built it, it´s ok. And that´s problem. Why Designer use default property and not that which is set? Thanks

Load is not called when a component is in the designer. Code in properties and in the constructor are. If you want changing the viewType property to cause a change in the designer you will need to use your code when the property is set. Instead of during load. That should also work for you at run time.
I would create a method like this:
private void UpdateViewType()
{
if (this._viewType == ViewType.Month)
panelKalendar.Top = yPoloha;
else if (this._viewType == ViewType.Week)
panelKalendar.Top = yPoloha + VYSKA_BUNKY;
}
and then in the property when the value is set call it
...
set
{
_viewType = value;
UpdateViewType()
if (panelKalendar != null)
panelKalendar.Invalidate();
}

Related

C# Windows Forms - How to get previous SelectedItem in Combobox

I'm currently trying to figure out how to get the previous Selected Item in a Combobox, the data is added in a list in the Form1_Load function.
//Flavour Change Button
private void CoboFlav_SelectionChangeCommitted(object sender, EventArgs e)
{
var selectedItemPrice = (coboFlav.SelectedItem as Flavour).price;
var selectedItemName = (coboFlav.SelectedItem as Flavour).name;
var pre_item = pre_selIndex = (coboFlav.SelectedItem as Flavour).price;
//var previousItem = flavourTea_Previous_Var = (coboFlav.SelectedItem as Flavour).price;
//Item List
Flavour listItem1 = ListCopy.MyList2.Find(x => (x.name == "- None -"));
Flavour listItem2 = ListCopy.MyList2.Find(x => (x.name == "Lemon"));
Flavour listItem3 = ListCopy.MyList2.Find(x => (x.name == "Passionfruit"));
Flavour listItem4 = ListCopy.MyList2.Find(x => (x.name == "Yogurt"));
//Checking Base Tea Box for adding price to currentItemTotal
if (coboFlav.Text == listItem1.name || coboFlav.Text == listItem2.name || coboFlav.Text == listItem3.name || coboFlav.Text == listItem4.name)
{
//Increment Item Cost Value & take away previous item cost
currentTotalItemCost += selectedItemPrice - pre_item;
}
//Update CUrrentTotal Text
CurrentTotal.Text = currentTotalItemCost.ToString();
}
If the user selected an option in the combobox the selectedPrice int is increased. I am trying to take away the previousItemCost and im having trouble understanding how to find the previous selected user input.
I am not really sure how to approach this, I have seen a couple of other people declare a new int as -1 and set the SelectedIndex to that. But I don't really understand that solution. If someone could point me in the right direction that would be awesome. Also I am quite new to windows forms as I came from a Unity background.
Thanks
Apparently you want a special kind of ComboBox. In your "object oriented programming" course you learned that if you want a class similar to another class, but with some special behaviour, you should create a derived class:
class PreviousSelectedComboBox : ComboBox // TODO: invent proper name
{
private int previousSelectedIndex = -1;
[System.ComponentModel.Browsable(false)]
public virtual int PreviousSelectedIndex {get; private set;}
{
get => this.previousSelectedIndex;
set => this.previousSelectedIndex = value;
}
[System.ComponentModel.Browsable(false)]
public override int SelectedIndex
{
get => base.SelectedIndex;
set
{
if (this.SelectedIndex != value)
{
this.PreviousSelectedIndex = this.SelectedIndex;
base.SelectedIndex = value;
// TODO: call OnSelectedIndexChanged?
}
}
}
}
Test In a dummy test program or a unit test, check if ComboBox.OnSelectedIndexChanged is called by base.SelectedIndex, If not, call it in the SelectedIndex.Set.
Also check what happens if ComboBox.SelectedItem.Set is called. Does this change the selected index by calling your overridden property SelectedIndex?
Event: I don't think that you need an event PreviousSelectedIndexChanged. It won't add anything, because this event is raised whenever event SelectedIndexChanged is raised, so those who want to get notified when PreviousSelectedIndex changes, could subsbribe to event SelectedIndexChanged.
Still, if you want such an event, follow the pattern that is used in property SelectedIndex and in OnSelectedIndexChanged.

Is it better to put my logic in an event handler or in a setter for MVVM (Xamarin `Picker` `SelectedItem` quirks)

SOLUTION IS IN EDIT OF THE ACCEPTED ANSWER
I have a view in which has two Pickers, I need to have it so that when the SelectedItem property in one Picker changes, the list of Items in the second Picker (ItemSource) changes as well.
Currently I have a bound the SelectedItem and SelectedIndex properties of both pickers to properties in my ViewModel. In the setter(s) for both of them, I perform the logic needed to change the list of Items in the second picker. The list of Items in the second picker changes successfully, but when I set the SelectedIndex (to make it select an Item by default), this fails if the index which I am setting it to is the same as the index which it was on in the previous list. It just shows the Title of the Picker instead, this issue seems to be related to this bug.
My Code:
Both Pickers are bound to an Observable collection of strings FYI
FrameType and DirectionType are Enums
I initially used only the SelectedItem property
Relevant XAML
<Label Grid.Row="1" Grid.Column="0" VerticalTextAlignment="Center"
Text="Direction: " />
<Picker x:Name="PickerDirection" Grid.Row="1" Grid.Column="1"
Title="Select Direction"
ItemsSource="{Binding Directions}"
SelectedItem="{Binding SelectedDirection}"
SelectedIndex="{Binding SelectedDirectionIndex}"></Picker>
<Label Grid.Row="2" Grid.Column="0" VerticalTextAlignment="Center"
Text="Frame: "/>
<Picker x:Name="PickerFrame" Grid.Row="2" Grid.Column="1"
Title="Select Frame"
ItemsSource="{Binding Frames}"
SelectedItem="{Binding SelectedFrame}"
SelectedIndex="{Binding SelectedFrameIndex}"></Picker>
Relevant View Model code:
public string SelectedDirection
{
get { return _selectedDirection; }
set
{
if (Directions.Contains(value))
{
if (_selectedDirection != value)
{
if (EnumUtils.ToEnumFromString<FrameType>(SelectedFrame) == FrameType.Road &&
!DirectionsRoad.Contains(value))
{
_selectedDirection = Directions[Directions.IndexOf(DirectionType.Right.ToString())];
}
else
{
_selectedDirection = value;
}
OnPropertyChanged();
}
}
}
}
public string SelectedFrame
{
get { return _selectedFrame; }
set
{
if (_selectedFrame != value)
{
_selectedFrame = value;
if (EnumUtils.ToEnumFromString<FrameType>(_selectedFrame) == FrameType.Road)
{
Directions = DirectionsRoad;
if (Directions.Contains(SelectedDirection))
{
SelectedDirectionIndex = Directions.IndexOf(SelectedDirection);
}
else
{
SelectedDirectionIndex = Directions.IndexOf(DirectionType.Right.ToString());
}
}else if (EnumUtils.ToEnumFromString<FrameType>(_selectedFrame) == FrameType.Lane)
{
Directions = DirectionsAll;
SelectedDirectionIndex = Directions.IndexOf(SelectedDirection);
}
OnPropertyChanged();
}
}
}
}
public int SelectedDirectionIndex
{
get { return _selectedDirectionIndex; }
set
{
if (_selectedDirectionIndex != value)
{
if (!(value < 0 || value >= Directions.Count))
{
_selectedDirectionIndex = value;
OnPropertyChanged();
}
}
}
}
public int SelectedFrameIndex
{
get { return _selectedFrameIndex; }
set
{
if (_selectedFrameIndex != value)
{
if (!(value < 0 || value >= Frames.Count))
{
_selectedFrameIndex = value;
OnPropertyChanged();
}
}
}
}
The outcome:
I expect it to never be empty since I ensure that the SelectedDirection is always set to something.
Notes:
I initially used just the SelectedItem property, but when I encountered this bug I thought using the SelectedIndex property would help to fix it.
I used ObservableCollection to maintain consistency with the other viewModels in the project, and to ensure that the options in the picker are updated when I make changes (based on my understanding you need to use ObservableCollection to make this possible).
I do plan to refactor the code in the setter for SelectedFrame into smaller functions as soon as I get things to work.
Due to this It seems that using the SelectedIndexChanged event of the Picker would be the only way to fix this. However the comment by ottermatic in this question says that events are unreliable. Hence I felt is was better to perform this logic in the setter.
If someone could comment on what I may be doing wrong in my code which is causing this issue and also comment on the pros/cons and/or whether or not I should use the eventHandler or have the logic in my setter. Thanks
I don't really see why you are using both SelectedItem and SelectedIndex, but I think what you are trying to achieve can be achieved easier.
First of all, I don't think you need ObservableCollection types at all in your example, since you are setting the directions anyway and not modifying the collection. More importantly, fiddling around with the indices is completely unnecessary, as far as I can tell. You are using strings anyway and even though String is not a value type, but a reference type, you cannot practically distinguish two String instances that have the same content, hence assinging the respective values to SelectedDirection and SelectedFrame is sufficient.
The following checks seem redundant to me
if (Directions.Contains(value))
if (EnumUtils.ToEnumFromString<FrameType>(SelectedFrame) == FrameType.Road &&
!DirectionsRoad.Contains(value))
since Directions are set to DirectionsRoad anyway if SelectedFrame has been set to "Road". Hence I'd assume that the second condition won't evaluate to true in any case. Hence the SelectedDirection can be simplified:
public string SelectedDirection
{
get => _selectedDirection;
set
{
if (_selectedDirection != value && Directions.Contains(value))
{
_selectedDirection = value;
OnPropertyChanged();
}
}
}
Within the setter of SelectedFrame there are many things going on, which I'd refactor to methods on it's own right, to improve clarity.
public string SelectedFrame
{
get => _selectedFrame;
set
{
if (_selectedFrame != value)
{
_selectedFrame = value;
UpdateAvailableDirections();
OnPropertyChanged();
}
}
}
private void UpdateAvailableDirections()
{
// store the selected direction
var previouslySelectedDirection = SelectedDirection;
Directions = GetValidDirectionsByFrameType(EnumUtils.ToEnumFromString<FrameType>(SelectedFrame));
SelectedDirection = GetSelectedDirection(previoslySelectedDirection, Directions);
}
private string[] GetValidDirectionsByFrameType(FrameType frameType)
{
return frameType == FrameType.Road ? DirectionsRoad : DirectionsAll;
}
private string GetSelectedDirection(string previouslySelectedDirection, string[] validDirections)
{
return validDirections.Contains(previouslySelectedDirection) ? previouslySelectedDirection : DefaultDirection;
}
By setting the SelectedItem instead of fiddling with the indices, the correct values shall be displayed.
Concerning your question whether this logic may be better suited in an event handler or in the setter depends on your requirements. If all you need is the index, the event SelectedIndexChanged may work out for you, but if the value is needed in several places and methods that are not called by the event handler, the presented solution may be more viable.
Edit
You were correct, it has got nothing to do with the usage of SelectedIndex and SelectedItem. The issue is a bit more subtle.
I build a quick proof-of-concept and found the following:
Assuming SelectedDirection is "Right" (and the index is set accordingly)
When Directions is set, the SelectedItem on the picker seems to be reset
SelectedDirection is set to null
this.Directions.Contains(value) evaluates to false, hence _selectedDirection is not set (this hold true for SelectedDirectionIndex, since the value -1 is filtered by if(!value < 0 || value >= this.Directions.Count))
When SelectedDirection is set afterwards, the value is still "Right", hence OnPropertyChanged is not called (since the values are the same) and the SelectedItem is not set
This way there is a mismatch between the value the Picker actually holds and the property in the viewmodel, which leads to unexpected behavior.
How to mitigate the issue?
I'd still stick with the code without the indices (unless you really need them) and use the string values.
There are other possibilities, but I'd change the setter of SelectedDirection. When you allowed the value to be set to null, PropertyChanged will be raised properly when the value is set to Right afterwards. If you really need to filter what the value is set to, you should still raise OnPropertyChanged, to inform the Picker that the value has changed (preventing a mismatch between the actual Pickers value and the viewmodel)
public string SelectedDirection
{
get => _selectedDirection;
set
{
if (_selectedDirection != value)
{
if(Directions.Contains(value))
{
_selectedDirection = value;
}
else
{
_selectedDirection = DirectionTypes.Right.ToString();
}
OnPropertyChanged();
}
}
}
Found a somewhat hacky fix for this, and it seems to be a Xamarin issue. I have made the following changes to my code"
The relevant changes are in the setter for SelectedFrame:
public string SelectedFrame
{
get { return _selectedFrame; }
set
{
if (_selectedFrame != value)
{
_selectedFrame = value;
if (EnumUtils.ToEnumFromString<FrameType>(_selectedFrame) == FrameType.Road)
{
Directions = DirectionsRoad;
if (Directions.Contains(SelectedDirection))
{
/*Relevant edits*/
var position = Directions.IndexOf(SelectedDirection);
SelectedDirection = Directions[Directions.Count - position -1];
SelectedDirection = Directions[position];
}
else
{
SelectedDirectionIndex = Directions.IndexOf(DirectionType.Right.ToString());
}
}else if (EnumUtils.ToEnumFromString<FrameType>(_selectedFrame) == FrameType.Lane)
{
Directions = DirectionsAll;
/*Relevant edits*/
var position = Directions.IndexOf(SelectedDirection);
SelectedDirection = Directions[Directions.Count - position -1];
SelectedDirection = Directions[position];
}
OnPropertyChanged();
}
}
}
}
It seems that my issue arises when I change the contents of my ObservableCollectoin but the SelectedDirection stays the same.
When I change Directions (which is an ObservableCollection) by assigning it to DirectionsAll (also an ObservableCollection), I need to make sure that the SelectedDirection changes,. The added code ensures that a change actually occurs to SelectionDirection and that fixes it. Seems somewhat hacky but it works.
Outcome:

calling a dispatcher to cancel a MVVM property change does not work

I have a MVVM property to update and there are some situations where I want to cancel the change inside the setter.
I have read several SO posts explaining that we need to use a dispatcher to do this:
link2
link1
However, it does not work in my case.
For any reason, the XAML control still updates.
I am confused as I can see the debugger run the dispatcher method after the properrty is set to the new value. It does run the code that should switch it back, but the control sticks to the new value instead of switching back to the old one.
My code is as follows:
private DB_VisionParameters _MyProp;
public DB_VisionParameters MyProp
{
get
{
return _MyProp;
}
set
{
if (_MyProp == value)
{
return;
}
if ( isInvalidTest)
{
MyType storedValue = _MyProp;
//switch back to update interface with old value
DB_VisionParameters previousVisionParameter = _MyProp;
if (DispatcherHelper.UIDispatcher != null)
DispatcherHelper.UIDispatcher.BeginInvoke(
(new Action(() =>
{
_MyProp = storedValue;
RaisePropertyChanged("MyProp");
})), DispatcherPriority.ContextIdle);
return;
}
_MyProp = value;
RaisePropertyChanged("MyProp");
}
}
Looks like SO solutions to me but I have probably missed something :(
I tried your code and if MyProp has the right value (i.e. the old value) and if it is not modified by anyone else then it should update.
As you have also confirmed the same that you had it being modified in your version of code thus the instance was never the same and it reflected with newer version details on UI, which was not as expected.

(ASP.NET User control) - problems setting properties

I am creating several .NET User Controls and I am trying to figure out the best way to go about setting properties. I have an address control and I am trying to create a property called ShowCountry which will either hide or show the control's country ddl.
I have been trying to set most of my properties similar to the below code:
public bool ShowCountry
{
get { return (bool)ViewState["ShowCountry"]; }
set
{
ViewState["ShowCountry"] = value;
pnlCountry.Visible = value;
}
}
How would I set a default value for this property? When I run my page with the control on it, it instantly errors out in the "get{}" when ShowCountry is used in one of my functions because I never set ShowCountry="false" in the control's tag. If I set this property when declaring the control everything works fine.
Also is what I am doing with the ViewState a good way to keep property values across postbacks?
Could someone show me how they would write this property?
The specs are:
Must keep value across postbacks, Must default to false
you can try this in order to avoiding error..
public bool ShowCountry
{
get {
if(ViewState["ShowCountry"] != null ){
return (bool)ViewState["ShowCountry"];
}
else {
//return the default value
return false;
}
}
set
{
ViewState["ShowCountry"] = value;
pnlCountry.Visible = value;
}
}
i think view state is best approach alternatively you can use hidden field in order to save value against post back.

PropertyChanged event fires but UI not update

public string TenNguoiDung
{
get { return _currentNguoiDung.TenNguoiDung; }
set
{
_currentNguoiDung.TenNguoiDung = value != null
? value.ToStandardString(true) : string.Empty;
SendPropertyChanged("TenNguoiDung");
ValidProperty(_currentNguoiDung.TenNguoiDung, new
ValidationContext(_currentNguoiDung) { MemberName = "TenNguoiDung" });
}
}
public static string ToStandardString(this string value,
bool isAllStartWithUpper = false)
{
string result = string.Empty;
value = value.Trim();
var listWord = value.Split(' ').ToList();
listWord.RemoveAll(p => p == string.Empty);
foreach (var item in listWord) result +=
item.Substring(0, 1).ToUpper() + item.Substring(1).ToLower() + " ";
if (!isAllStartWithUpper) result =
result.Substring(0, 1) + result.Substring(1).ToLower();
return result.Trim();
}
I have a TextBox:
<TextBox Grid.Column="1" Margin="1" Text=
"{Binding Path=TenNguoiDung,Mode=TwoWay,NotifyOnValidationError=True}"/>
When i typed some clear text to Standard, Setter called SendPropertyChanged("TenNguoiDung") but on UI not update. How can i fix that??
Edit:
public void SendPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
Edit:
I debuged at Setter, i saw _currentNguoiDung.TenNguoiDung changed after ToStandardString, but on UI was not updated
Update
I used few hours to google and i got my answer.
I feel happy when this bug only happen on debug mode. When i run application without debug, it work perfect!!
Fix debug Mode:
[link]Coerce Value in Property Setter - Silverlight 5
Right click on the Web project in Solution Explorer and select
Properties.
Select the Web tab.
Scroll down to the Debuggers section.
Uncheck the checkbox labelled Silverlight.
Make sure you are setting the data context to the instance of the class containing your property.
From the looks of it you are doing this but make sure you are implementing INotifyPropertyChanged
When debugging if you have any binding errors happening these will show in the output window.
Is the value changing if you type in 'hello' and then replace it with 'world'? If so it may just be a case sensitivity issue

Categories

Resources