EventHandler in WPF - Change in one element affects another - c#

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

Related

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.

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

Is it possible to type in dashes if I have a TextBox that is a Child of a Checkbox?

So I have this currently:
<CheckBox x:Name="checkBox" HorizontalAlignment="Left" Margin="346,17,0,0" Grid.Row="1" VerticalAlignment="Top" Width="295" Height="189" >
<TextBox x:Name="textBox" HorizontalAlignment="Left" Height="20" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
</CheckBox>
I currently can not type in a "-" in my TextBox because it is a child of my CheckBox and CheckBox has some built in feature where if the +/= key OR -/_ key is pressed it checks/unchecks my CheckBox. I don't want this feature, I want the user to be able to actually type in dashes like "-" without it unchecking/checking my checkbox. Is there a easy way to get around this?
Thanks in advance
Wow, after hours of losing my mind I figured out how to get rid of this problem.
All you have to do is set IsThreeState = True; because they are doing a check for IsThreeState in the CheckBox.cs code here
One way is to create a custom checkbox and override OnKeyDown:
public class MCheckBox : CheckBox
{
protected override void OnKeyDown(KeyEventArgs e) { }
}
Another way is to just not put the TextBox within the CheckBox, but beside it, both in a DockPanel with some modifications:
<StackPanel>
<CheckBox>
<TextBox Text="this is a TextBox within a CheckBox"/>
</CheckBox>
<DockPanel LastChildFill="False">
<CheckBox VerticalAlignment="Center"/>
<TextBox Text="this is a TextBox beside a CheckBox" Margin="4,0,0,0"/>
</DockPanel>
</StackPanel>

Why is my Visibility binding not working?

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}

Silverlight: ComboBox Behavior

really simple problem, but I guess I have just the wrong definition of combobox:
I'd like to get a simple thing like:
http://www.c-sharpcorner.com/uploadfile/mahesh/combobox-in-silverlight/
But whenever I add a combobox (or a listbox) and set the itemssource, it shows directly all items and I dont have a textbox-like selection.
My approach was quite simple:
In XAML I define:
<StackPanel Orientation="Horizontal">
<TextBlock VerticalAlignment="Center" Style="{StaticResource styleStdWidth}" Text="Spieler 1:" />
<ListBox x:Name="lsbPlayerOne" ItemTemplate="{StaticResource dtName}" Width="300" />
<TextBox x:Name="txtPlayerOnePoints" Style="{StaticResource stylePlayerWidth}" />
</StackPanel>
<DataTemplate x:Name="dtName">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" TextWrapping="Wrap" FontSize="35" FontWeight="Bold" x:Name="txbname"/>
</StackPanel>
</DataTemplate>
And in Code behind I just set the ItemsSource with a List, which has data.
Since the ListBox gets bigger every time I add a item, it gets uglier and uglier.
Am I missing a property, which I didnt find? I did not see anything...
Sorry for the confusing question :)
P.S.: I tried the same as in the example shown in the link. Sadly I cant open the sample project.
Matthias Müller
Your question is unclear. But you are not implementing a combobox in the code you have shown. Why don't you use a combobox and set your itemsource to the list that contains the fields you want to use?
<ComboBox ItemSource={Binding Names}/>

Categories

Resources