2012-05-12 9 views
6

Tengo un bloque de texto cuyo atributo de texto es vinculante para un DateTime? escriba datos, y quiero mostrar algo cuando el DateTime? los datos son nulos.
El siguiente código funciona muy bien.
Cómo enlazar una cadena localizada en el atributo TargetNullValue?

< TextBlock Text="{Binding DueDate, TargetNullValue='wow,It's null'}"/> 

Pero ¿qué pasa si quiero enlazar un Localizedstring a la TargetNullValue? ?.
el código de abajo no funciona :(
Cómo

< TextBlock Text="{Binding DueDate, TargetNullValue={Binding LocalStrings.bt_help_Title1, Source={StaticResource LocalizedResources}} }"/> 

Respuesta

3

no veo ninguna manera de hacerlo con TargetNullValue Como solución, puede intentar utilizar un convertidor:

public class NullValueConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     if (value != null) 
     { 
      return value; 
     } 

     var resourceName = (string)parameter; 

     return AppResources.ResourceManager.GetString(resourceName); 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

a continuación, agregarlo a los recursos de su página:

<phone:PhoneApplicationPage.Resources> 
    <local:NullValueConverter x:Key="NullValueConverter" /> 
</phone:PhoneApplicationPage.Resources> 

Por último, en lugar de usarlo TargetNullValue:

<TextBlock Text="{Binding DueDate, Converter={StaticResource NullValueConverter}, ConverterParameter=bt_help_Title1}" /> 
+0

Al agregar todos estos, un error cuando se construye, { 'local' es un prefijo no declarado.} :( –

+0

Esto se debe a que se supone que declare 'xmlns:. Locales = "clr-namespace: NamespaceOfYourProject" '(en el teléfono: nodo PhoneApplicationPage de su página, con los otros xmlns) –

+0

Muchas gracias, ¡funciona genial! –

1

Dado que no puede tener una encuadernación dentro de otra encuadernación, deberá usar una encuadernación múltiple.

Algo así como:

<Window.Resources> 
    <local:NullConverter x:Key="NullConverter" /> 
</Window.Resources> 

<TextBlock> 
    <TextBlock.Text> 
     <MultiBinding Converter="{StaticResource NullConverter}"> 
      <Binding Path="DueDate"/> 
      <!-- using a windows resx file for this demo --> 
      <Binding Source="{x:Static local:LocalisedResources.ItsNull}" /> 
     </MultiBinding> 
    </TextBlock.Text> 
</TextBlock> 

public class NullConverter : IMultiValueConverter 
{ 
    #region Implementation of IMultiValueConverter 

    public object Convert(object[] values, Type targetType, 
          object parameter, CultureInfo culture) 
    { 
     if (values == null || values.Length != 2) 
     { 
      return string.Empty; 
     } 

     return (values[0] ?? values[1]).ToString(); 
    } 

    public object[] ConvertBack(object value, Type[] targetTypes, 
           object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 

    #endregion 
} 
Cuestiones relacionadas