I have a Button, and I want the button to be visible only when Condition A == true and Condition B == true.
Now, my viewModel already have two Property whose return type is boolean.
So is it possible to achieve it with XAML Binding?
If you want your button to be visible depending on two properties then in this case it would be better you make a Visibility Property in your Viewmodel and bind that property to Button Visibility Property in Xaml.
For example :-
In Viewmodel create Visibility property -
private Visibility _visBtn = Visibility.Collapsed;
public Visibility VisBtn
{
get { return _visBtn ; }
set
{
_visBtn = value;
RaisePropertyChanged("VisBtn "); // INotifyPropertyChanged Implemented
}
}
You have to just set this property according to your logic. like :-
If(Condition A == true && Condition B == true)
VisBtn = Visibility.Visible;
Now Bind this property to Button In Xaml Like -
<Button Content="My Button" Visibility="{Binding VisBtn }" />
Note :- Make sure that you have implemented INotifyPropertyChanged In your Viewmodel and Your Xaml Page DataContext is set to corresponding Viewmodel properly.
Second Case :-
If you want to set your button Visibility on basis of single bool property then you can implement BooleanToVisibility Converter this converter map Boolean property to Visibility type.
Then How to implement boolean to visibility Converter help you.
Related
In UWP, I want to handle and skip the tab key navigation for particular set of controls dynamically.
For e.g., I've two user controls (Both have lot of children which will be added dynamically) in my main page and want to skip tab key navigation for one usercontrol dynamically on specific scenario for a moment.
So I've tried to set "IsTabStop" as false to that UserControl. But which is not effective on its child controls. Still tab key focus moved inside the children of that UserControl.
Note: If I set "IsEnabled" as false, then its working. But I don't want to use because it affects Visual appearance.
Thanks in advance.
There is no IsTabStop property in StackPanel class. The IsTabStop property is defined in Windows.UI.Xaml.Controls.Control class, and the StackPanel class is not inherited from Control class, either directly or indirectly. You can not use IsTabStop property to set a StackPanel instance.
Therefore, if you want to skip tab key navigation for one stackpanel, you need to set the IsTabStop property to False in each control in the stackpanel.
Update:
By testing, the child elements in a UserControl can not inherit the value of IsTabStop property. Therefore, you cannot skip tab key navigation for all the child elements in a UserControl by setting the IsTabStop property to False.
You could use a method defind in your UserControl class to set IsTabStop to false for every item in your UserControl.
For example:
MyUserControl.cs
public void SetIsTabStop(bool flag)
{
var result = VisualTreeFindAll<Control>(this);
foreach (var item in result)
{
item.IsTabStop=flag;
}
}
private IList<T> VisualTreeFindAll<T>(DependencyObject element)
where T : DependencyObject
{
List<T> retValues = new List<T>();
var childrenCount = VisualTreeHelper.GetChildrenCount(element);
for (var i = 0; i < childrenCount; i++)
{
var child = VisualTreeHelper.GetChild(element, i);
var type = child as T;
if (type != null)
{
retValues.Add(type);
}
retValues.AddRange(VisualTreeFindAll<T>(child));
}
return retValues;
}
Use the name of a UserControl instance to call the SetIsTabStop(bool flag) method with False.
userControlName.SetIsTabStop(false);
Update:
Maybe you could try the following workaround.
Add the KeyDown event handler for the UserControl in your Page.
private void UserControl_KeyDown (object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Tab)
{
e.Handled = true;
button1.Focus(FocusState.Keyboard);
}
}
You need to let the first control except the UserControl and all its child controls get the focus, then the key navigation will skip the child controls in the UserControl except the first child control in the UserControl.
I am trying to combine two concepts, a extending a standard control, and application theming by extending the Picker control and adding in a TextColor property and having a custom renderer on the Android (and iOS eventually) platform. I can successfully set the TextColor property and have it display the color if it is set statically as the following:
<controls:ExtendedPicker TextColor="Red"/>
The final step in this process is to be able to pull the property from a Dynamic Resource and have it able to be changed at runtime.
<controls:ExtendedPicker Style="{DynamicResource pickerStyle}"/>
and then, in the Application.Resources ResourceDictionary:
<Color x:Key="textColor"/>
...
<Style x:Key="pickerStyle" TargetType="controls:ExtendedPicker">
<Setter Property="TextColor" Value="{DynamicResource textColor}" />
</Style>
Note that this method for choosing TextColor works with native controls such as Label. The code compiles and runs but doesn't seem to pick up the dynamic resource color setting when changed at runtime. I am assuming that this is something that I am missing handling this in my custom renderer but am at a loss as to what to look for.
To handle this at run-time I had to override the
OnElementPropertyChanged
method in the custom renderer rather than the
OnElementChanged
method.
Override the OnElementChanged your custom platform-dependent render and you can get a reference to your Xamarin.Forms-based custom (subclassed) control via the e.NewElement property.
So you can cast e.NewElement as your custom control and get that classes properties (your custom Picker color that you assigned in the XAML):
(e.NewElement as ExtendedPicker).TextColor;
Something like this:
protected override void OnElementChanged (ElementChangedEventArgs< ExtendedPicker> e)
{
base.OnElementChanged (e);
if (Control == null) {
~~~~~~
}
if (e.OldElement != null) {
~~~~~~
}
if (e.NewElement != null) {
var myPickerControl = (e.NewElement as ExtendedPicker). TextColor;
// Assign `myPickerControl` to your UI dependent control
~~~~~~~
}
}
I've implemented a UWP SplitView similar to the one made by Diederik Krols. I prefer the approach of using a ListView over using RadioButtons as shown by Jerry Nixon's implementation of the SplitView.
However, I have a problem when I add secondary commands at the bottom of the SplitView, which Diederik doesn't do. These secondary commands are implemented by another ListView bound to a collection of Commands. So I have TWO ListViews that should only allow ONE item to be selected among them at a time.
I've tried two things:
I've bound the SelectedItem property of both ListViews to the same object. The idea was that maybe ListView doesn't display a selection if SelectedItem is not in the list bound to ItemsSource. Sadly, it simply goes on displaying the last selected item.
I've bound each ListView's SelectedItem to its own property. When one of the ListViews' item is selected, the SelectedItem of the other property is set to 'null' by the ViewModel. This produces the same result as in 1.
Any ideas on how to solve this problem?
I had the same problem.
I have a fix, but I'm not that proud of it ;) it's a dirty hack and I'm hoping other solutions will present itself so I can change it too.
But here it is:
First the listviews hook up to the SelectionChanged event even though we also bind the selected item to the viewmodel ( full code shown here https://github.com/AppCreativity/Kliva/blob/master/src/Kliva/Controls/SidePaneControl.xaml )
<ListView x:Name="TopMenu"
SelectionChanged="OnTopMenuSelectionChanged"
Background="Transparent"
ItemsSource="{x:Bind ViewModel.TopMenuItems}"
ItemTemplateSelector="{StaticResource MenuItemTemplateSelector}"
ItemContainerStyle="{StaticResource MenuItemContainerStyle}"
SelectedItem="{x:Bind ViewModel.SelectedTopMenuItem, Mode=TwoWay, Converter={StaticResource XBindItemCastingConverter}}"
Grid.Row="0" />
In that SelectionChanged, we'll deselect the 'other' listviews selection! ( full code shown here https://github.com/AppCreativity/Kliva/blob/master/src/Kliva/Controls/SidePaneControl.xaml.cs )
Note that we need to keep track that we are already in a deselecting process otherwise we'll end up with an endless loop. This is done with the _listViewChanging field.
private void OnBottomMenuSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (!_listViewChanging)
{
_listViewChanging = true;
TopMenu.SelectedIndex = -1;
_listViewChanging = false;
}
}
Last thing to do is making sure we handle the selection and clear it again in the viewmodel for next itteration ( full code shown here https://github.com/AppCreativity/Kliva/blob/master/src/Kliva/ViewModels/SidePaneViewModel.cs )
public MenuItem SelectedBottomMenuItem
{
get { return _selectedBottomMenuItem; }
set
{
if (Set(() => SelectedBottomMenuItem, ref _selectedBottomMenuItem, value))
{
if (value != null)
{
if (string.IsNullOrEmpty(SelectedBottomMenuItem.Title))
HamburgerCommand.Execute(null);
if (SelectedBottomMenuItem.Title.Equals("settings", StringComparison.OrdinalIgnoreCase))
SettingsCommand.Execute(null);
SelectedBottomMenuItem = null;
}
}
}
}
I got a UserControl name UC1, which contain 2 controls (textbox, label). after that i create new windows form and drag the UC1 to it, how can i read and write ALL properties of textbox and label (inside UC1) in "Properties Windows" in design-mode? currently, i only can see UC1's properties, but properties of textbox and label never show out. please ... thanks..
REMARKS: No want create 1 by 1 of properties of control for example: Public Overrides Property blabla As String... End Property
in control.cs
[Description("Label displayed in the Control"), Category("Controls")]
public Label lblCaption
{
get { return _lblCaption; }
set { _lblCaption = value; }
}
in control.Designer.cs
public System.Windows.Forms.Label _lblCaption;
do this for button
I have placed a background image on my windows application form and when tab stops to a particular button it's color is changed and looks awkward...Can anyone tell me that how can I set some customized color for tabstop or set its value to null????
I've tried the answer from BalaR i.e. button.ShowFocusCues = falase in load event of the form but it says that it can't be used like this and it is protected
try ShowFocusCues to false
button.ShowFocusCues = false;
to hide focus rectangle.
I didn't notice it was a protected member. You have two options
use reflection to set the protected member (not recommended)
create a derived class and set the protected member as you'd like. (the right way)
class MyButton : Button
{
protected override bool ShowFocusCues
{
get { return false; }
}
}
You can also set TabStop to false.
button.TabStop = false;
if you don't want the button to obtain focus during tab cycle.