Why is my Visibility binding not working? - c#

I have a problem with one of my Visibility Bindings.
In my application I have a Textbox and a combobox at nearly the same place.
They are overlapping so I have two Variables in the background for their Visibility. They are in a Datagrid, so the Visibility Source will be provided from the list.
The Binding of my Combobox works absolutely fine, but the one of my Textbox doesn't
Here is my Code:
<TextBox x:Name="Textvalue"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Width="150"
BorderBrush="#FF383F55"
BorderThickness="0"
Foreground="White"
Background="#FF232734"
Text="{Binding Path=Value, UpdateSourceTrigger=PropertyChanged}"
Visibility="{Binding Path=IsPath}"/>
<ComboBox x:Name="Combobox"
VerticalAlignment="Center"
Foreground="White"
Margin="3,3,3,3"
Height="23"
ItemsSource="{Binding Path=ValueArray}"
SelectedValue="{Binding Path=Value, UpdateSourceTrigger=PropertyChanged}"
Style="{DynamicResource ComboBoxStyle1}"
Visibility="{Binding Path=IsCombobox}"/>
These are my Get Property for both (Both only have the Get Proberty):
public System.Windows.Visibility isPath
{
get
{
return _IsPath;
}
}
public System.Windows.Visibility IsCombobox
{
get
{
return _IsCombobox;
}
}
I hope you can help me because I don't know what's wrong here :/
Thanks in advance
What I have tried:
Tried to change the binding to a different Variable.

The property name in your code has a lower case 'i' public System.Windows.Visibility isPath and the XAML has an upper case I in binding IsPath. Change this and it should be working!
{Credit C S, I'd already started writing this}

Related

EventHandler in WPF - Change in one element affects another

I have a stackpanel with a TextBlock and a ComboxBox and the I have a TextBox acting as a search field.
<StackPanel Grid.Row="1" Grid.Column="2">
<TextBlock Text="Group Filter" Margin="7,0,0,0"/>
<ComboBox Text ="{Binding GroupFilter}" Height="30" Width="130" Margin="0,4,0,0"
Padding="5" IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Source={StaticResource GroupFilterEnum}}"
</ComboBox>
</StackPanel>
<TextBox Text="{Binding SingleFilter, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Row="1" Grid.Column="3" Height="30" Width="140" Margin="0,20,0,0" Padding="5" materialDesign:HintAssist.Hint="Search single Id...">
</TextBox>
So basically I have a data set and what you can do is you can filter the data based on either a predefined setting from theGroupFilterEnum or you can enter a filter value yourself in the TextBox.
What I try to do is:
When the TextBox is empty things should just be as they are. When the TextBox has a value I would like to add the following to the ComboBox to indicate that the ComboBox is inactive when there is a value in the TextBox:
IsEditable="False"
IsHitTestVisible="False"
Focusable="False"
Foreground="{StaticResource MaterialDesignBodyLight}"
I have tried to make an TextChanged EventHandler for the TextBox like:
private void textChangedEventHandler(object sender, TextChangedEventArgs args)
{
var text = sender.ToString().Replace("System.Windows.Controls.TextBox: ", "");
if (!string.IsNullOrWhiteSpace(text))
{
}
}
But I don't really know how to refer to the ComboBox from this EventHandler. I am fairly new to this, so I haven't reallty done a lot of EventHandlers. Is this the way to do it? If yes, could someone please help me with the Eventhandler?
Or maybe there is a better way? Directly in the xaml file maybe?
All help is highly appreciated and please let me know if additional information is needed.
Thanks in advance.
You could bind the ComboBox properties you want to change to the Text property of the TextBox using a converter that you would need to implement (here is the example).
<TextBox x:Name="MyTextBox"/>
<ComboBox Focusable="{Binding ElementName=MyTextBox, Path=Text, Converter={StaticResource EmptyStringToBooleanConverter}}">
If you want a control that can be used like a textBox or comboBox, you are not in the good way.
The comboBox control have a property IsEditable which permit to write or select a value.
Try this : <ComboBox IsEditable="True"/>
Else, you can give a name to your control and you will able to get it in your code behind by typing his name.
The result here : Result
(And I'm sorry for my english, I'm french).

WPF Clear combobox text value when checkbox is unchecked

I want to clear the combobox text value or set it to another string whenever the checkbox is uncheck. Is there any good way to bind them together in xaml ?
Thanks.
Maybe this helps. It is solved with bindings:
<Grid>
<ComboBox IsEditable="{Binding ElementName=Box, Path=IsChecked}" HorizontalAlignment="Left" Margin="211,167,0,0" VerticalAlignment="Top" Width="120">
<ComboBoxItem>Test</ComboBoxItem>
<ComboBoxItem>Test2</ComboBoxItem>
</ComboBox>
<CheckBox x:Name="Box" Content="Clear" HorizontalAlignment="Left" Margin="131,171,0,0" VerticalAlignment="Top"/>
</Grid>
If you want to change the text, it may be necessary to write a converter:
Text="{Binding ElementName=Box, Path=IsChecked, Mode=OneWay, Converter={StaticResource TextConverter}}"
Take a look at Microsoft Documentation about how to write a value converter.

Cannot set combobox selected item opening a view

I created a new project using template10 and I'm working to a simple form: I load a list of items from a remote server in a comboxbox and select one of the items after the list is loaded.
I tried setting SelectedValue, SelectedItem ot SelectedIndex, but when the Form is shown the listbox appears unselected.
Am I missing something?
this is the xaml
<ComboBox x:Name="voceSpesaCb" Margin="16,16,0,0"
RelativePanel.AlignLeftWith="parameterResizer"
RelativePanel.Below="voceSpesaTextBlock"
DisplayMemberPath="Descrizione"
SelectedValue="{x:Bind ViewModel.VoceCorrente, Converter={StaticResource XConverter}, Mode=TwoWay}"
ItemsSource="{x:Bind ViewModel.Voci, Converter={StaticResource XConverter}}"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
this is the code
Voci = await vociSpesaTable.OrderBy(vs => vs.Descrizione).ToListAsync();
VoceCorrente = Voci.FirstOrDefault(vs => vs.VoceSpesaNo == Item.VoceSpesaNo);
in the setter of the properties there is the call to the RaisePropertyChanged
Looks loike I found the problem. For reasons I don't understand at full I have to set Mode=TwoWay for the ItemsSource property too.
<ComboBox x:Name="voceSpesaCb" Margin="16,16,0,0"
RelativePanel.AlignLeftWith="parameterResizer"
RelativePanel.Below="voceSpesaTextBlock"
DisplayMemberPath="Descrizione"
SelectedValue="{x:Bind ViewModel.VoceCorrente, Converter={StaticResource XConverter}, Mode=TwoWay}"
ItemsSource="{x:Bind ViewModel.Voci, Converter={StaticResource XConverter}, Mode=TwoWay}"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
/>
This way it works

Combobox text property twoway binding not working

I have a ComboBox as below
<ComboBox VerticalAlignment="Center"
Width="83.84"
Canvas.Left="626.24"
Canvas.Top="249.088" DataContext="{Binding Items[0]}"
Text="{Binding TextVariable, Mode=TwoWay, NotifyOnValidationError=True, TargetNullValue='', ValidatesOnDataErrors=True, ValidatesOnExceptions=True}"
Height="68.293"
Style="{StaticResource ComboBoxDialogControlQ69_74}" />
The problem is: when I set the value for TextVariable in my view model it gets displayed on UI. But when I change it on UI it is not updated in the property.
I think I am missing something very obvious, any help is appreciated.
It works really good when i tried doing like below for Text binding on ComboBox,
Text="{Binding TextVariable, Mode=TwoWay, UpdateSourceTrigger=LostFocus, NotifyOnValidationError=True, TargetNullValue='', ValidatesOnDataErrors=True, ValidatesOnExceptions=True}"
I have added one more property to make it editable.
IsEditable="True"
if you need the change on PropertyChange change UpdateSourceTrigger to PropertyChanged
UpdateSourceTrigger=PropertyChanged
VM
public string TextVariable {
get
{ return _TextVariable; }
set
{
_TextVariable = value;
NotifyPropertyChanged();
}
}

Textbox not updating

Ive tried searching, but maybe im not using the right terms to search for.
I have several textboxes that im using, and when i enter data, i see the values being updated when debugging, but they never get updated to the form.
Basically, I have a form that is used for the visuals, and then i have a class that handles all the activity. I have created the class as a resource, and I am referencing to the resource within the textboxes.
The only way that i really knew how to handle the updating of the forms was by implementing the calculations on values changing. So if I updated a property, I called the method from OnPropertyChanged(). This created issues because the values were always getting changed due to the calculations rewriting values. I then tried evaluating the changes of the new value
I.E.
public double In1
{
get{_return _in1;}
set{
if (_in1 != value)
_in1 = value;
OnPropertyChanged("In1");
}
}
regardless of anything, my problem is that i dont see the values getting written to the textbox. This is my first real endevour of using data binding so im assuming that im doing something incorrectly (clearly)
<ad:DockableContent
xmlns="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="DMC_Robot_Editor.GUI.frmAngleConvertor"
Title="frmAngleConvertor" Height="259" Width="282">
<ad:DockableContent.Resources>
<local:AngleConvertor x:Key="Converter"/>
</ad:DockableContent.Resources>
<Grid >
<GroupBox HorizontalAlignment="Stretch" VerticalAlignment="Top">
<Grid>
<ComboBox x:Name="cbInput" HorizontalAlignment="Stretch" VerticalAlignment="Top" Grid.Column="1" Grid.ColumnSpan="3" Grid.Row="1" DisplayMemberPath="ValueCartesianString" SelectedValuePath="ValueCartesianEnum" IsSynchronizedWithCurrentItem="True" SelectedIndex="{Binding InputItem,Source={StaticResource Converter}}" ItemsSource="{Binding InputConvention, Source={StaticResource Converter}}" IsReadOnly="True"/>
<TextBox x:Name="tbIn1" HorizontalAlignment="Center" VerticalAlignment="Bottom" Text="{Binding In1, Converter={StaticResource DoubleToStringConverter}, Source={StaticResource Converter}}" Grid.Column="0" d:LayoutOverrides="GridBox" Grid.Row="2" Width="50" TextAlignment="Center">
<TextBox.DataContext>
<local:AngleConvertor/>
</TextBox.DataContext> </Grid>
</ad:DockableContent>
public class AngleConverter()
{
private double _in1 = 0.0;
public double In1
{
get{_return _in1;}
set{
if (_in1 != value)
_in1 = value;
OnPropertyChanged("In1");
}
}
}
Try to apply UpdateSourceTrigger=PropertyChanged on your text box:
Text="{Binding Path-In1, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource DoubleToStringConverter}, Source={StaticResource Converter}}"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
You can add to your binding UpdateSourceTrigger=PropertyChanged with your Mode TwoWay
<TextBox Name="tbIn1"
HorizontalAlignment="Center"
VerticalAlignment="Bottom"
Text="{Binding In1, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,
Converter={StaticResource DoubleToStringConverter},
Source={StaticResource Converter}}"
Grid.Column="0"
d:LayoutOverrides="GridBox"
Grid.Row="2"
Width="50"
TextAlignment="Center"
/>
your real code should have something like this:
public class AngleConverter : INotifyPropertyChanged
so i assume its just a typo in your code. you did not post your converter code, the binding in your textbox is ok. Textbox default UpdateSourceTrigger is lostfocus. so maybe UpdateSourceTrigger=PropertyChanged did what you want.
Review the binding
Does DoubleToStringConverter get called?
Is get called?
Text="{Binding In1, Converter={StaticResource DoubleToStringConverter}, Source={StaticResource Converter}}"
Move Source out of binding in into DataContext on the DockableContent.
DataContext="{Binding RelativeSource={RelativeSource self}}"
Try with no converter
Text="{Binding In1}"

Categories

Resources