Pregunta

Actualmente estoy aprendiendo cómo desarrollar y construir una aplicación para Windows Phone 7.

Si un determinado valor es cierto, tengo que añadir un TextBlock al cuadro de lista antes de que un TextBlock (diga su nombre es x:Name="dayTxtBx").

Actualmente estoy usando

dayListBox.Items.Add(dayTxtBx);

para agregar el cuadro de texto.

Cualquier ayuda muy apreciada!

Gracias

¿Fue útil?

Solución

Esto es bastante fácil de hacer si usted está utilizando una DataTemplate y una ValueConverter y pasar todo el objeto en el cuadro de lista (en lugar de sólo una cadena). Suponiendo que haya algún objeto que se parece a:

public class SomeObject: INotifyPropertyChanged
{
    private bool mTestValue;
    public bool TestValue 
    {
        get {return mTestValue;}
        set {mTestValue = value; NotifyPropertyChanged("TestValue");}
    }
    private string mSomeText;
    public string SomeText
    {
        get {return mSomeText;}
        set {mSomeText = value; NotifyPropertyChanged("SomeText");}
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string name)
    {
        if ((name != null) && (PropertyChanged != null))
        {
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }
}

Se puede hacer un convertidor que se parece a:

public class BooleanVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value != null && (bool)value)
            return Visibility.Visible;
        else
            return Visibility.Collapsed;
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

y añadir el convertidor a su XAML de esta manera:

<UserControl x:Class="MyProject.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MyProject">
    <UserControl.Resources>
        <local:BooleanVisibilityConverter x:Key="BoolVisibilityConverter" />
    <UserControl.Resources>

A continuación, podría tener el cuadro de lista definida en XAML, así:

<Listbox>
  <Listbox.ItemTemplate>
    <DataTemplate>
      <StackPanel Orentation="Horizontal" >
        <TextBlock Text="Only Show If Value is True" Visibility={Binding TestValue, Converter={StaticResource BoolVisibilityConverter}} />
        <TextBlock Text="{Binding SomeText}" />
      </StackPanel>
    </DataTemplate>
  </Listbox.ItemTemplate>
</Listbox>

Podría parecer mucho, pero en realidad es bastante simple una vez a empezar. Una gran manera de aprender más sobre el enlace de datos y convertidores está en el blog de Jesse Liberty ( http: / /jesseliberty.com/?s=Windows+Phone+From+Scratch ).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top