How to specify an empty string as Target null value in xaml - c#

From here https://msdn.microsoft.com/en-us/library/system.windows.data.bindingbase.targetnullvalue(v=vs.110).aspx,
It gives an example of specifying 'TargetNullValue':
<TextBox Width="150"
Text="{Binding Source={StaticResource object2},
Path=PropertyB, BindingGroupName=bindingGroup,
TargetNullValue=please enter a string}" />
My question is how can I specify an empty string for TargetNullValue?
I have tried "TargetNullValue= }" (there is a space between = and }, but that does not work, the target null value is null, instead of an empty string.
Thank you.

You can use x:Static in the xaml and define the string.empty there.
<TextBox Width="150"
Text="{Binding Source={StaticResource object2},
Path=PropertyB, BindingGroupName=bindingGroup,
TargetNullValue={x:Static system:String.Empty} }" />
You'll need to add the appropriate namespace to the xaml as needed. VS should do this for you though. Namespace required is
xmlns:system="clr-namespace:System;assembly=mscorlib"

I don't know why the answer saying to use '' is deleted, but it works fine for me:
<TextBox Text="{Binding NumberOfCats, TargetNullValue=''}" />

Related

How do I pass string with spaces to converterParameter?

My sample code is below.
I want to pass 'Go to linked item' to ConverterParameter but I can't because the string has spaces.
Text="{Binding Value,
Source={x:Static local:Dictionary.Instance},
Converter={StaticResource StringConverter},
ConverterParameter=Go to linked item, Mode=OneWay}"
How can I do this?
Option 1
Text="{Binding Value,
Source={x:Static local:Dictionary.Instance},
Converter={StaticResource StringConverter},
ConverterParameter='Go to linked item', Mode=OneWay}"
Option 2
If you want to use this in multiple places add a string resource.
<sys:String x:Key="GoToLink">Go to linked item</sys:String>
And pass the resource key.
ConverterParameter={StaticResource ResourceKey=GoToLink}}
If your string has spaces then wrap it in single quotes, double quotes won't work; this is probably due to the fact that the entire text field is wrapped in double quotes and so using them again within the binding would incorrectly indicate closure.
Text="{Binding Value,
Source={x:Static local:Dictionary.Instance},
Converter={StaticResource StringConverter},
ConverterParameter='Go to linked item', Mode=OneWay}"

Bind object's children to TextBox

I using Entity Framework to manage my connection to the DB.
I have this example object:
Company
UniqueCode
Name
Address
I'll search the database using the UniqueCode to find a Company and after finding one, I want to show Name and Address on a couple of TextBoxes.
The example Xml is this one:
<TextBox x:CodeTextBox ...>
<Grid x:Name="ValuesGrid">
<TextBox x:Name="NameTextBox" ...>
<TextBox x:Name="AddressTextBox" ...>
</Grid>
And later, if the user modifies anything, I'll save the values to the DB.
So, what's the best way to show values of an object in a bunch of TextBoxes?
I'm thinking about using DataContext on the ValuesGrid, but I don't know how to make it work.
<Grid x:Name="ValuesGrid" DataContext="{Binding Company}">
<TextBox x:Name="NameTextBox" Text="{Binding Name}">
<TextBox x:Name="AddressTextBox" Text="{Binding Address}">
</Grid>
Where Company needs to be non-private property.

How do I, or can I use a static resource for the StringFormat on a TextBlock?

I have a text block that is displaying the date/time. The look of the clock may be different on some controls in the application, as far as color and maybe font, but I want the date and time to have the same format.
I know I can set the StringFormat property like so:
<TextBlock Text="{Binding CurrentDateTime, StringFormat='{}{0:h\:mm tt}'}" Foreground="White" FontFamily="Proxima Nova Rg" FontSize="20" />
However, I don't know how to pull the string format out into a separate string resource dictionary. I tried to do something like the following, but the date time string didn't appear at all.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib">
<system:String x:Key="MyFormat">{}{0:h\:mm tt}</system:String>
</ResourceDictionary>
<!-- In another file -->
<TextBlock Text="{Binding CurrentDateTime, StringFormat={StaticResource MyFormat}}" Foreground="White" FontFamily="Proxima Nova Rg" FontSize="20" />
Can this be done at all? If so, how?
Thanks
It appears you just have to remove the {}:
<system:String x:Key="MyFormat">{0:h\:mm tt}</system:String>
The {} is required for attribute values to prevent them from being interpreted as a markup extension. However, with property element syntax, you no longer need the {} because curly braces do not have special meaning in that context.

Issue with displaying images

OK, here's my issue :
I've got a custom class, with a list of items - each of which has an image path associated with it
I've added a folder WITH the images inside the project (so I suppose there are being added in the XAP too, huh?)
When I'm trying to bind the Source of an image item in XAML, it's not working.
<ListBox Margin="0,0,-12,0" ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,0,0,17">
<Image Height="100" Width="100" Margin="12,0,9,0" Source="/AlbumArt/{Binding AlbumArt}"/>
<StackPanel Width="311">
<TextBlock Text="{Binding Title}" TextWrapping="Wrap" Style="{StaticResource PhoneTextLargeStyle}"/>
<TextBlock Text="{Binding Author}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
What am I doing wrong? Any ideas?
P.S.
I've also tried Source="{Binding AlbumArt}" but it's still not displaying anything.
The craziest thing is that, if I set the Source to a specific image (e.g. /AlbumArt/someImage.jpg), the image seem to work fine in Visual Studio & the Emulator.
I don't think Binding works that way. If it is not possible to prepend the string "/AlbumArt/" to your image path property (which is AlbumArt), I would suggest using a converter to do so.
Also, formatting strings only works when the target property is a string so StringFormat is out. Someone correct me if I'm wrong about StringFormat
If you have binding issues always check the output window for details. That is where information about binding errors is displayed.
This Source="/AlbumArt/{Binding AlbumArt}" won't work because it will just be treated as a string.
Without seeing your class it's hard to be certain but the property you're binding to ("AlbumArt") should be a Uri and not a string and it should be populated with a relative Uri to the image. That image should also be set to having the build action of Content.
what you have to is this..
<Image Height="100" Width="100" Margin="12,0,9,0" Source="{Binding ImagePath}"/>
as you said you have list of items( i think it would be class that also having paths of images ) so make this property in this class and for every item put the path in this property for coresponding item.
private string _ImagePath;
public string ImagePath
{
get
{
return _ImagePath;
}
set
{
_imagePath = value;
}
}
it would be better if you implement INotifyPropertyChanged in your item class.
How about getting more nesty.
private string _AlbumArt;
public string AlbumArt
{
get
{
return _AlbumArt;
}
set
{
if(_AlbumArt!=null)
_AlbumArt=#"/AlbumArt/"+ value;
}
}
and Binding
<Image Height="100" Width="100" Margin="12,0,9,0" Source="{Binding AlbumArt}"/>
I'm pretty sure you've just used the wrong path, try this one....
"/ApplicationName;component/AlbumArt/{Binding AlbumArt}"
Replace the ApplicationName section with your applications name of course. MAKE SURE TO REPLACE SPACES IN IT WITH %20

How do I use Interactivity:Interaction.Triggers on DatePicker

I am trying to bind SelectedDateChanged to a command so I can place it inside my ViewModel but nomatter what then it wont accept this code
http://pastebin.com/T4q8hQBA
<DatePicker Name="HistoryDateTime" Grid.Column="1" Grid.Row="0" HorizontalAlignment="Center" DisplayDateStart="{Binding Path=StartDate}" DisplayDateEnd="{Binding Path=EndDate}" SelectedDate="{Binding Path=SelectedDate}" >
<Interactivity:Interaction.Triggers>
<Interactivity:EventTrigger EventName="SelectedDateChanged" >
<Helpers:InvokeDelegateCommandAction Command="{Binding DataContext.SaveKategorieCommand, ElementName=LayoutRoot}" CommandParameter="{Binding}"/>
</Interactivity:EventTrigger>
</DatePicker>
I know line 4 is wrong but even with that removed none of those lines are accepted, so do I need to include a file or something for Interactivity to work?
This is C# and WPF not silverlight btw
You probably need to add to your namespace declaration the following one:
xmlns:interactivity="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
And add a reference to System.Windows.Interactivity to your project.
For Windows-8.1 application development we should add below line
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"

Categories