I have a datatemplate containing an image that I want to be hidden if the the value of a property in a ViewModel is true. Can anyone tell me why the the xaml below does not work?
<Image x:Name="img" Source="..\Images\List_16.png" Margin="0,0,5,0">
<Image.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding CurrentListHasPendingChanges}" Value="True">
<Setter Property="Image.Visibility" Value="Hidden" />
</DataTrigger>
<DataTrigger Binding="{Binding CurrentListHasPendingChanges}" Value="False">
<Setter Property="Image.Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
Try removing "Image" part from Property="Image.Visibility" so you'll have:
<Setter Property="Visibility" Value="Hidden"/>
and add TargetType to your Style:
<Style TargetType="{x:Type Image}">
I just did something similar using a ContentControl.
<ContentControl Content="{Binding CurrentListHasPendingChanges}">
<ContentControl.ContentTemplate>
<DataTemplate>
<Image x:Name="img" Source="..\Images\List_16.png" Margin="0,0,5,0" Visibility="Hidden" />
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding}" Value="False">
<Setter Property="Image.Visibility" Value="Visible" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>
From http://karlhulme.wordpress.com/2007/03/06/using-a-contentcontrol-and-datatemplate-to-indicate-new-andor-modified-data/
isn't that
<Setter Property="Visibility" Value="Hidden" />
?
I assume you use INotifyProptyChanged.
EDIT I did some Googling and I think you need to use some sort of template in order to make the trigger work.
eg.: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/ae2dbfb7-5dd6-4352-bfa1-53634289329d
http://www.thejoyofcode.com/Help_Why_cant_I_use_DataTriggers_with_controls_in_WPF.aspx
In my opinion we not need to use Triggers, with only the Binding it works well.
To make binding to a property model, you can use BooleanToVisibilityConverter
Is declared as follows:
<UserControl.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</UserControl.Resources>
And how to use it is simple, just point to the key stated above:
<Image HorizontalAlignment="Left" Height="16" VerticalAlignment="Center" Width="16"
Visibility="{Binding HasError, Converter={StaticResource BooleanToVisibilityConverter}}"
Source="/myPath;component/Resources/Images/image1.png"/>
The property in the ViewModel:
private bool _hasError = false;
public bool HasError
{
get { return !string.IsNullOrEmpty(_messageError); }
set
{
_hasError = value;
this.NotifyOfPropertyChange(() => this.HasError);
}
}
Related
In my wpf usercontrol, I have a toggle button like this,
<ToggleButton x:Name="btnExpand" Grid.Row="0" Width="25" Height="25" HorizontalAlignment="Right" Margin="5" >
<Image Width="20" Height="20">
<Image.Style>
<Style TargetType="{x:Type Image}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsCheckedState}" Value="true">
<Setter Property="Source" Value = "pack://application:,,,/MyAssembly;component/SalesModule/Images/expand.png"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsCheckedState}" Value="false">
<Setter Property="Source" Value = "pack://application:,,,/MyAssembly;component/SalesModule/Images/collapse.png"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</ToggleButton>
But the images are not showing !!! Both Image's BuildAction is already set to Resource in their properties. Then i cleaned the solution and then rebuilded. Still no use.
Could you please help me to display the image inside the toggle button ??
Bind to the IsChecked property of the parent ToggleButton, and use only one DataTrigger and a regular Style Setter:
<ToggleButton x:Name="btnExpand" Grid.Row="0" Width="25" Height="25" HorizontalAlignment="Right" Margin="5" >
<Image Width="20" Height="20">
<Image.Style>
<Style TargetType="{x:Type Image}">
<Setter Property="Source" Value="pack://application:,,,/MyAssembly;component/SalesModule/Images/collapse.png"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked, ElementName = btnExpand}" Value="true">
<Setter Property="Source" Value="pack://application:,,,/MyAssembly;component/SalesModule/Images/expand.png"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</ToggleButton>
I have the following xaml:
<DockPanel>
<DockPanel>
<CheckBox IsChecked="{Binding Path=Test}" />
<CheckBox IsChecked="{Binding Path=Test}" />
</DockPanel>
<DockPanel DockPanel.Dock="Left" Width="10" Background="Blue">
<DockPanel.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Test}" Value="True">
<Setter Property="DockPanel.Background" Value="Yellow" />
</DataTrigger>
</Style.Triggers>
</Style>
</DockPanel.Style>
</DockPanel>
</DockPanel>
Now - the 2 checkboxes link properly - checking one will check the other - but the datatrigger is not firing at all.
What am I doing wrong?
The issue here is Property Value Precedence.
You are currently setting the Background to blue directly on the DockPanel. This explicit property will override any value set by the trigger.
Instead, you must set the original "Background" as a setter in the style.
<DockPanel DockPanel.Dock="Left" Width="10">
<DockPanel.Style>
<Style>
<Setter Property="DockPanel.Background" Value="Blue" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Test}" Value="True">
<Setter Property="DockPanel.Background" Value="Yellow" />
</DataTrigger>
</Style.Triggers>
</Style>
</DockPanel.Style>
</DockPanel></DockPanel>
I want to have dynamic editable listbox in wpf application. I am using textbox inside listbox, and I am binding Observable collection to that listbox. Now, I want to make textbox editable on mouse click. So, user can make change to textbox and save new textbox text.
<ListBox Name="ListTwo" ItemsSource="{Binding CollectionUrl, Mode=TwoWay}" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox Name="TextBoxList" Text="{Binding Path=urlString}" IsEnabled="False" >
</TextBox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
You should use IsReadOnly property. In the trigger you should check IsFocused property.
In the following example, I changed foreground color to indicate which TextBox is in the edit mode.
Example:
<ListBox Name="ListTwo" ItemsSource="{Binding CollectionUrl, Mode=TwoWay}" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox Name="TextBoxList" Text="{Binding Path=urlString}" MinWidth="100">
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Foreground" Value="Green"/>
<Setter Property="IsReadOnly" Value="False" />
</Trigger>
<Trigger Property="IsFocused" Value="False">
<Setter Property="Foreground" Value="Red"/>
<Setter Property="IsReadOnly" Value="True" />
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
If you want to allow users save changes after edit value in the TextBox, you can add button and show in the actual editing row:
<ListBox Name="ListTwo" ItemsSource="{Binding CollectionUrl, Mode=TwoWay}" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="50" />
</Grid.ColumnDefinitions>
<TextBox Name="TextBoxList" Text="{Binding Path=urlString}" MinWidth="100">
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Foreground" Value="Green"/>
<Setter Property="IsReadOnly" Value="False" />
</Trigger>
<Trigger Property="IsFocused" Value="False">
<Setter Property="Foreground" Value="Red"/>
<Setter Property="IsReadOnly" Value="True" />
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
<Button Content="Save" Grid.Column="1" Command="{Binding SaveChanges}">
<Button.Style>
<Style TargetType="Button">
<Setter Property="Visibility" Value="Collapsed" />
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=TextBoxList, Path=IsFocused}" Value="True">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
My approach to accessing the text box (which was proving a nightmare) was to use your save button approach and in the Button's Button_Click function I used sender to retrieve the Button's parent, cast to (in your case) Grid. Then you can use that to access the Grid's Children with .Children[0] being your TextBox.
Bit of a Kluge because your code has to 'know' the type of the parent and the index of the child TextBox but these will not change. If necessary the purists can iterate through the Children to identify the required child explicitly.
Hope this helps someone.
I'm new to WPF and I trying to create xaml logic to show / hide a control based on the value of the AllowMiscTitle on the ViewModel. The xaml consist of two fields a combobox of the standard tiles ("Mr", "Mrs", ..., "Other") when "Other" is selected I want the textbox to display.
I've created the follow xaml:
<DockPanel Validation.Error="Validation_Error" HorizontalAlignment="Stretch">
<ComboBox ItemsSource="{Binding Path=Titles, Mode=OneTime}"
Text="{Binding Path=Title}"/>
<TextBox x:Name="TxtBxTitle" Margin="5,5" Visibility="Visible">
<TextBox.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=AllowMiscTitle}" Value="false">
<Setter Property="TextBox.Visibility" Value="Hidden"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
</DockPanel>
That Trigger won't work because you have set Visibility property explicitly in TextBox
Do it like this:
<TextBox x:Name="TxtBxTitle" Margin="5,5">
<TextBox.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=AllowMiscTitle}" Value="false">
<Setter Property="TextBox.Visibility" Value="Hidden"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
The reason for this is Dependency property value precedence.
There is a
<BooleanToVisibilityConverter x:Key="BoolToVis"></BooleanToVisibilityConverter>
You can use it as following
<TextBox Visibility="{Binding YourPropertyName, Converter={StaticResource BoolToVis}}"></TextBox>
If I got your question right:-
If your selected value is binded to some property in the ViewModel like:-
private string _GenderType;
public string GenderType
{
get
{
return _GenderType;
}
set
{
_GenderType= value;
RaisePropertyChanged("GenderType");
In xaml:-
<TextBox.Style>
<Style>
<Setter Property="TextBox.Visibility" value="Hidden"/>
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=GenderType,ElementName=Combo1}" Value="Other">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
what is wrong about the following code?
I get this error during compilation:
The property 'TargetName' does not represent a valid target for the 'Setter' because an element named 'cc' was not found. Make sure that the target is declared before any Setters, Triggers or Conditions that use it.
How do I have to refactor my code so I can compile it without error?
I just want to switch a datatemplate with DataTrigger bound to a value in my PersonViewModel!
<ContentControl x:Name="cc" Grid.Column="1">
<DataTemplate>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=CurrentPersonViewModel.IsNew}" Value="True">
<Setter TargetName="cc" Property="ContentTemplate" Value="{DynamicResource NewPersonId}" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=CurrentPersonViewModel.IsNew}" Value="False">
<Setter TargetName="cc" Property="ContentTemplate" Value="{DynamicResource SelectedPersonId}" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ContentControl>
Update
You can use a Style for the ContentControl and change the ContentTemplate from there
<ContentControl Name="cc" Grid.Column="1">
<ContentControl.Style>
<Style TargetType="ContentControl">
<Setter Property="ContentTemplate" Value="{DynamicResource SelectedPersonId}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=CurrentPersonViewModel.IsNew}" Value="True">
<Setter Property="ContentTemplate" Value="{DynamicResource NewPersonId}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
UPDATE
I don't understand why the View's in the DataTemplate doesn't inherit the DataContext. Got it working by using this but I can't see why this is necessary
<DataTemplate x:Key="NewPersonId">
<local:NewPersonView DataContext="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContentControl}}, Path=DataContext.CurrentPersonViewModel}" />
</DataTemplate>
<DataTemplate x:Key="SelectedPersonId">
<local:SelectedPersonView DataContext="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContentControl}}, Path=DataContext.SelectedPersonViewModel}"/>
</DataTemplate>
You do not need the whole DataTrigger stuff.
Just read this to make your DataTemplateSelector work properly:
http://joshsmithonwpf.wordpress.com/2007/03/18/updating-the-ui-when-binding-directly-to-business-objects-that-are-modified/