在列表框中的另一个元素之前添加 TextBlock

发布于 2024-10-06 13:03:52 字数 267 浏览 1 评论 0原文

我目前正在学习如何开发和构建适用于 Windows Phone 7 的应用程序。

如果某个值为 true,我需要在 TextBlock 之前将 TextBlock 添加到 ListBox(假设其名称为 x:Name="dayTxtBx “)。

我目前正在使用

dayListBox.Items.Add(dayTxtBx);

添加文本框。

非常感谢任何帮助!

谢谢

I'm currently learning how to develop and building an app for windows phone 7.

If a certain value is true, I need to add a TextBlock to the ListBox before a TextBlock (say its name is x:Name="dayTxtBx").

I am currently using

dayListBox.Items.Add(dayTxtBx);

to add the text box.

Any help very much appreciated!

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时光的容颜 2024-10-13 13:03:52

如果您使用 DataTemplate 和 ValueConverter 并将整个对象传递到 ListBox(而不仅仅是一个字符串),那么这很容易做到。假设您有一些如下所示的对象:

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));
        }
    }
}

您可以创建一个如下所示的转换器:

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();
    }
}

并将转换器添加到您的 XAML 中,如下所示:

<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>

然后您可以在 XAML 中定义 ListBox,如下所示:

<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>

可能看起来很多,但它确实很漂亮一旦开始,就很简单。了解有关数据绑定和转换器的更多信息的一个好方法是访问 Jesse Liberty 的博客 ( http:// /jesseliberty.com/?s=Windows+Phone+From+Scratch )。

This is pretty easy to do if you're using a DataTemplate and a ValueConverter and passing the whole object into the ListBox (rather than just a string). Assuming you have some object that looks like:

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));
        }
    }
}

You can make a converter that looks like:

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();
    }
}

And add the converter to your XAML like so:

<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>

Then you could have the ListBox defined in XAML like so:

<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>

Might seem like a lot, but it's really pretty simple once you get started. A great way to learn more about data binding and converters is at Jesse Liberty's blog ( http://jesseliberty.com/?s=Windows+Phone+From+Scratch ).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文