WPF Clear combobox text value when checkbox is unchecked - c#

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.

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).

Autocomplete combobox vs2017 - properties

I have a problem with combobox. I do not have properties for him. Where can I find AutoComplete properties?
Typing also does not suggest anything to me and as I enter the whole thing, I get the error.
I'm explaining what I mean - I have a combobox with many list records.
I would like the combobox to open, for example, the first three letters.
this is my code of combobox
<ComboBox
x:Name="combolista"
HorizontalAlignment="Left"
Margin="10,384,0,0"
VerticalAlignment="Top"
Width="491"
Grid.Column="1"
SelectionChanged="ComboBox_SelectionChanged"
ItemsSource="{Binding Wyszukanie_miast}"
SelectedItem="{Binding WybraneMiasto}"
IsEditable="True"
TextSearch.TextPath="Miasto">
<ComboBox.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Miasto}" />
<TextBlock Text="Województwo" Margin="70,0,0,0"/>
<TextBlock Text="{Binding Wojewodztwo}" Margin="150,0,0,0"/>
</Grid>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
He works but I would like to enter all the possibilities in three letters
you can use telerik : RadAutoCompleteBox if you want, but telerik is not a free library.
you can also take a look at https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit

Telerik RadComboBox Apply SelectionBoxTemplate OnLoad?

Basically, we have created our own custom MultiSelect-ComboBox using Telerik's RadComboBox and the SelectionBoxTemplate to make the custom text appear when multiple items are selected.
<ucControls:MultiSelectComboBoxBase
... >
<ucControls:RadComboBox
x:Name="RadCombo"
Text=""
Height="22"
HorizontalContentAlignment="Left"
HorizontalAlignment="Stretch"
DropDownClosed="RadCombo_DropDownClosed"
IsTextSearchEnabled="False"
IsEnabled="{Binding IsEnabled}"
CanAutocompleteSelectItems="False"
CanKeyboardNavigationSelectItems="False"
LostFocus="RadCombo_LostFocus">
<ucControls:RadComboBox.SelectionBoxTemplate>
<DataTemplate>
<Grid Background="Aqua">
<TextBlock x:Name="ComboBoxDisplay" Text="{Binding Text, ElementName=RadCombo}" />
</Grid>
</DataTemplate>
</ucControls:RadComboBox.SelectionBoxTemplate>
<ucControls:RadComboBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<CheckBox x:Name="chkBox"
Content="{Binding ItemValue}"
Tag="{Binding ItemID}"
Height="16" Margin="2"
IsChecked="{Binding IsChecked, Mode=TwoWay}"
Visibility="{Binding IsSelectAllItem, Converter={StaticResource booleanToVisibilityConverter}, ConverterParameter=NOT}"
HorizontalAlignment="Stretch" VerticalAlignment="Top"
Checked="ChkBox_Checked" Unchecked="ChkBox_Unchecked"
IsEnabled="{Binding IsEnabled, Mode=TwoWay}"/>
<HyperlinkButton
VerticalAlignment="Center" Content="{Binding ItemValue}"
Visibility="{Binding IsSelectAllItem, Converter={StaticResource booleanToVisibilityConverter}}"
Style="{StaticResource HyperlinkButtonNoBorderStyle}" Margin="3,2,0,0"
Click="HyperlinkButton_Click"
IsEnabled="{Binding IsEnabled, Mode=TwoWay}" />
</StackPanel>
</DataTemplate>
</ucControls:RadComboBox.ItemTemplate>
</ucControls:RadComboBox>
It works great, but the only problem is that it appears that the SelectionBoxTemplate isn't actually applied when the radcombobox is loaded. It only applies when the box is clicked.When it loads it displays:
Then you click and it displays the dropdown:
Then you click away and it displays the correct text from the SelectionBoxTemplate:
Note I made the background color of the Textblock blue, just to show that it does not apply on the initial load.Ideally, when it loads it should automatically apply the SelectionBoxTemplate and display the correct information rather than having the click and then click away from the multiselectcombobox. Also, the TextBlock text is bound to the RadComboBox's text because in the code behind we set the RadComboBox's text. There is no issue with null values or the Text not being set before because I've looked in debugging mode and all the information is there, the selection box template is just not being applied until the box receives focus. Is there some sort of event that has to fire off before the template is applied, or is there someway I can force the template to be applied onload?
I had the same issue as you. I've managed to solve it by setting the SelectedIndex="0"
I've also read that the SelectionBoxTemplate only works when the RadComboBox is not editable: IsEditable="False"
<ucControls:RadComboBox
x:Name="RadCombo"
SelectedIndex="0"
IsEditable="False"
Height="22"
HorizontalContentAlignment="Left"
HorizontalAlignment="Stretch"
DropDownClosed="RadCombo_DropDownClosed"
IsEnabled="{Binding IsEnabled}"
LostFocus="RadCombo_LostFocus">
...
</ucControls:RadComboBox>

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}"

Combobox selections

I have this combobox which contains a data tempate with a checkbox and a textbox. All is working but I would like to make it a little bit easier to select a value. Right now I have to click on the chechbox to change the value of the check box. Now I would like to be able to just click on the item in the combobox which also should toggle the checkbox.
Is this possible? If yes then how?
Here is a picture of my solution right now
Here is the code for my combobox
<ComboBox Name="employeeComboBox" Margin="2,0,2,0"
ScrollViewer.CanContentScroll="False"
DataContext="{Binding EmployeesOverviewViewModel, Source={StaticResource ViewModelLocator}}"
ItemsSource="{Binding Employees}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding Path=IsSelected}" Margin="2,0,2,2" VerticalAlignment="Center"/>
<TextBlock Text="{Binding Path=Name}" VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Change your DataTemplate to this:
<DataTemplate>
<CheckBox IsChecked="{Binding Path=IsSelected}"
Margin="2,0,2,2"
Content="{Binding Path=Name}"
VerticalAlignment="Center"/>
</DataTemplate>
and it should work.
Why don't you use the Content property on the CheckBox?
<CheckBox Content="Hello, World" />
This way, the checkbox would toggle even when you click on the text (content).
As for your specific case, you can bind Name to Content instead of creating a separate TextBlock for it, and it should work as you want it to.
Whats happning here is, Your Checkbox and text are two different entities, you need to make them one, by simply using checkbox's text property and binding it. that way whenever you click on text your checkbox gets selected.

Categories

Resources