Set properties of text box when combobox selection is made WPF XAML - wpf

how to Set properties of text box when combobox selection is made . foe example set background and IsEnabled property of text box when a combo box selection is made. I want it Purely in XAML not in code behind. i use MVVM

How to enable textBox1 only when SelectedItems is 1
<TextBox Height="23" HorizontalAlignment="Left" Margin="246,177,0,0" Name="textBox2" VerticalAlignment="Top" Width="120">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="IsEnabled" Value="False"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=comboBox1, Path=SelectedIndex}" Value="1">
<Setter Property="Background" Value="Green"></Setter>
<Setter Property="IsEnabled" Value="True"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
<ComboBox Height="22" HorizontalAlignment="Left" Margin="246,119,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" />
I think only with XAML you cannot achieve the condition Value ="1" or "3", i.e. a relation in a data trigger more complex than a equality.
For this case you need a converter.
This link could help you
How to get DataTemplate.DataTrigger to check for greater than or less than?

You can use a datatrigger for combo's selected object. Have a look to this previous question: WPF Visibility of a UI element based on combo selection
Try to generate a trigger when selecteditem is {x:Null}. For that, you will need to put your controls inside a DataTemplate and the put the trigger in the template's triggers collection.
Here is a sample code (not tested, please check by your own):
<TextBox Height="23" HorizontalAlignment="Left" Margin="246,177,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" IsEnabled" Value="True" />
<ComboBox Height="22" HorizontalAlignment="Left" Margin="246,119,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" />
<DataTemplate.Triggers>
<Trigger SourceName="comboBox1" Property="ComboBox.SelectedItem" Value="{x:Null}">
<Setter TargetName="textbox2" Property="TextBox.IsEnabled" Value="False" />
</Trigger>
</DataTemplate.Triggers>

Related

How to enable a popup control in wpf when I click inside a textbox?

This is something I want to add as a feature. I've already made e a class that saves the last user search text into a .txt file. I also have it to when the user opens my application the list of searches get loaded into a list. However, what I need help with is displaying this list my xaml.
What I want to achieve for the user is when he/she clicks inside the search box, I want to load the list last saved searches. I wanted it to show up in a ListView inside a Popup control just below the search box. This is how I have my xaml set up
<TextBox Grid.Column="1"
Width="100"
Name="TextBox"
Text="{Binding SearchText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<TextBox.Resources>
<Style TargetType="{x:Type Popup}">
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="IsOpen" Value="True" />
</Trigger>
<Trigger Property="IsFocused" Value="False">
<Setter Property="IsOpen" Value="False" />
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Resources>
</TextBox>
<Popup Name="SavedSearchesList">
<ListView ItemsSource="{Binding SavedSearchesList, Mode=OneWay}"
SelectedItem="{Binding SavedSearchSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
</ListView>
</Popup>
However when I click inside the Text Box my popup control is not opening. What am I doing wrong? Many thanks in advance.
Remove the Trigger from the TextBox and try the following for your Popup:
<Popup Name="SavedSearchesList" IsOpen="{Binding IsFocused, ElementName=NameofTextBox}">
<ListView ItemsSource="{Binding SavedSearchesList, Mode=OneWay}"
SelectedItem="{Binding SavedSearchSelected, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}">
</ListView>
</Popup>
Keep in mind that you have to set a name for your TextBox and replace 'NameofTextBox' with that name.
Hope this works.

WPF set default Visibility value when editing

I have Visibility bound to a bool, which works perfectly. However when editing the page the Border is not visible. I have to delete the Visibility Binding, make my changes and redo the Visibility Binding.
I'm pretty sure I saw there is a way to set a "editing default", but I cannot find that link anymore (or remember what it was called). Can someone explain how to set the default to visible so I can see it whilst editing, but not affect it's operation at runtime?
<Border Grid.Column="2" BorderBrush="HotPink" BorderThickness="2" MinHeight="100" MinWidth="100"
Visibility="{Binding ElementName=GenerateWorkOrders, Path=IsChecked, Converter={StaticResource booleanToVisibility}, UpdateSourceTrigger=PropertyChanged}">
<Label Content="Not Visible While Editing"/>
</Border>
The problem is that the default value of IsChecked of GenerateWorkOrders CheckBox is false
If IsChecked have Binding, you can use FallbackValue:
<CheckBox x:Name="GenerateWorkOrders" IsChecked="{Binding SomeProperty, FallbackValue=True}" />
Another way is to avoid the binding, you can use the DesignerProperties.IsInDesignMode Attached Properties that indicate if you in design mode (More inforamtion).
You can use this property in behavior, or in XAML only approach:
<Border Grid.Column="2" BorderBrush="HotPink" BorderThickness="2" MinHeight="100" MinWidth="100">
<Border.Style>
<Style TargetType="{x:Type Border}">
<Setter Property="Visibility" Value="{Binding ElementName=GenerateWorkOrders, Path=IsChecked, Converter={StaticResource booleanToVisibilityConverter}, UpdateSourceTrigger=PropertyChanged}" />
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=(componentModel:DesignerProperties.IsInDesignMode)}" Value="true">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
<Label Content="Not Visible While Editing"/>
</Border>

How to set the Control Visibilty of the Second Control depending on the Binding Value of the first

I have a Custom Wpf Control i.e. combobox:WpfTwComboBox. I want to set the visibility using a property called DisableProviderSelector.
The usual use of triggers is not helping. The scenario here is when the above control i.e. WindowsFormsHost is made visible or collapsed, I want the opposite to happen to the below custom control.
<StackPanel Grid.Row="3" Grid.Column="2" Height="25" Orientation="Horizontal"
Width="375" HorizontalAlignment="Left">
<WindowsFormsHost Height="25" Width="375">
<WindowsFormsHost.Style>
<Style TargetType="WindowsFormsHost">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=DisableProviderSelector}" Value="true">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=DisableProviderSelector}" Value="false">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</WindowsFormsHost.Style>
<commonControls:ProviderSelectorControl RequiredLevel="Save" ModifiedByUser="providerSelectorControl1_ModifiedByUser" x:Name="providerSelectorControl1"/>
</WindowsFormsHost>
<combobox:WpfTwComboBox x:Name="PortalProviderSelector"
SelectedValue="{Binding SelectedPortalProvider}"
ItemsSource="{Binding Path=PortalProvidersCollection}"
DisplayMemberPath="FullName" Width="350" Height="25"
RequiredLevelFlag="Save">
</combobox:WpfTwComboBox>
</StackPanel>
Can anyone please help me on how to set the visibility here?
So DisableProviderSelector is a bool when set to True WindowsFormsHost needs to be Collapsed and ComboBox needs to be Visible. Reverse when bool is false.
So as far as the ComboBox is concerned if bool is True it's Visible and when False it's Collapsed. Thus just bind the ComboBox directly to the Property and use a BooleantoVisibilityConverter
xaml:
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
</Window.Resources>
...
<combobox:WpfTwComboBox x:Name="PortalProviderSelector"
Width="350"
Height="25"
DisplayMemberPath="FullName"
ItemsSource="{Binding Path=PortalProvidersCollection}"
RequiredLevelFlag="Save"
Visibility="{Binding DisableProviderSelector,
Converter={StaticResource BooleanToVisibilityConverter}}"
SelectedValue="{Binding SelectedPortalProvider}" />

set default string name on combobox

I am using a Combobox whose ItemSource is ObservableCollection(i.e. ConversationList) of type .
<ComboBox x:Name="ConvId"
Grid.Row="2"
Width="75"
Height="23"
Margin="6,94,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
ItemsSource="{Binding ConversationList,
UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding SelectedId,
Mode=TwoWay}">
My requirement is : when there is no int value in Collection then Combobox Left side should display 'ConvId' which is a string. Fig. is shown below
I have workaround i.e to Convert collection from int to string and put 'ConvId' on 0th location and mark SelectedIndex= 0. but its not we want.
Do I have to use some Custom control for this. Is there any to acheive this in XAML.
This Stack thread seems to do what you want cleanly with a Converter.
How to display default text "--Select Team --" in combo box on pageload in WPF?
The answer I am referring to starts with this:
<Grid>
<ComboBox
x:Name="comboBox1"
ItemsSource="{Binding MyItemSource}" />
<TextBlock
Visibility="{Binding SelectedItem, ElementName=comboBox1, Converter={StaticResource NullToVisibilityConverter}}"
IsHitTestVisible="False"
Text="... Select Team ..." />
</Grid>
Have below textblock below your combobox and make sure that both combobox and textblock are overlapping each other(i.e. they should be in same grid row)
<TextBlock Text="ConvID"
IsHitTestVisible="False">
<TextBlock.Style>
<Style
TargetType="TextBlock">
<Setter
Property="Visibility"
Value="Collapsed" />
<Style.Triggers>
<DataTrigger
Binding="{Binding ConversationList.Count}"
Value="0">
<Setter
Property="Visibility"
Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
Hope this helps.
Have a look at the WatermarkService by John Myczek. You can have it display a default string if no item is selected in the ComboBox. There's some issues with ComboBox in the answer I linked to, but if you look further down there's a fix for that.

wpf binding to element

Well i guess it's easy my scenario is having 2 elements:
ListBox and Button:
<ListBox Name="BannedItemsListBox"
Margin="5"
MinWidth="100"
MaxWidth="100" " Height="
204" ItemsSource="{Binding Path=BannedItems, Mode=TwoWay}"></ListBox>
<Button Name="RemoveBannedItemsButton"
Margin="5"
MinWidth="65"
Height="22"
Click="RemoveBannedItemButton_Click">Remove</Button>
I want to bind the IsEnabled property button to be true only if Item from ListBox is selected (focused) in XAML
I tried
IsEnabled="{Binding ElementName=BannedSourcesListBox, Path=TouchesDirectlyOver.Count}"
but no go.
What does the selection have to do with the Touches? (Also the ElementName is off)
I would try this:
IsEnabled="{Binding SelectedItems.Count, ElementName=BannedItemsListBox}"
Explanation: Basically the binding system tries to convert the input to the property at hand, a boolean, so when it gets an integer, 0 will be converter to false, anything higher to true. So the Button will be enabled if one or more items are selected.
<Button Content="Button"
Height="23"
HorizontalAlignment="Left"
Margin="138,12,0,0"
Name="button1"
VerticalAlignment="Top"
Width="75"
Click="button1_Click">
<Button.Style>
<Style>
<Setter Property="Button.IsEnabled"
Value="True" />
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=lstTest , Path=SelectedItem}"
Value="{x:Null}">
<Setter Property="Button.IsEnabled"
Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>

Resources