Reputation: 305
Let's say I have
<TextBox
x:Name="txtBox1" />
<!-- more XAML here -->
<TextBox
x:Name="associatedToTextBox1"
IsEnabled={Binding ElementName=txtBox1, Path=Text, Magic=txtBox.Text != string.Empty} />
I want associatedToTextBox1
to be enabled only when txtBox1
is not empty. I thought there was a way to embed that functionality into xaml, without converters. Is this possible? If so, how?
Upvotes: 0
Views: 268
Reputation: 602
I have recently been trying to achieve something like this. Made some progress and success with Roslyn analyzers but wasn't satisfied. Needed something much simpler and easier to use.
Ended up creating a markup extension which does exactly as you mentioned in your question. Currently the nuget is in beta. Soon will be released. You can check out haley.mvvm (versions 6.3.1 and above..)
Nuget: https://www.nuget.org/packages/Haley.MVVM/6.3.0
Source code: https://github.com/TheHaleyProject/HaleyMVVM/blob/development/HaleyMVVM/Extensions/DataTriggerExtension.cs
This may not be a clean solution. Lots of work needs to be done in future but currently With this you can achieve, something like below.
<Button x:Name="btnMain" Background="yellow" Height="50" Content="Click Me" />
<TextBlock FontSize="24" Background="WhiteSmoke" TextAlignment="Center" Margin="10"
Text="{hm:Trigger Path=IsMouseOver,Value=True,OnSuccess='Hovered Over Self', FallBack='hello world'}"
Foreground="{hm:DataTrigger Path={Binding ElementName=btnMain,Path=Background},Value='#FF008000',OnSuccess='darkred',FallBack='green'}">
Upvotes: 0
Reputation: 128061
There is nothing like an "inline expression".
You may however use a DataTrigger in a TextBox Style:
<TextBox x:Name="associatedToTextBox1">
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<DataTrigger Binding="{Binding Text, ElementName=txtBox1}"
Value="">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
Upvotes: 2