如何将所选项添加到文本块

时间:2014-01-07 04:47:59

标签: windows-phone

我正在开发一款Windows手机应用程序。 我为months创建了一个列表框。当用户点击任何month(例如,5月)时,文本块应该获得该值。

我的问题是;如何将此选定值添加到文本块?

1 个答案:

答案 0 :(得分:0)

试试这个:

XAML:

    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
        <TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
        <TextBlock Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <ListBox SelectionChanged="ListBox_SelectionChanged" Name="ListBox"/>
    </Grid>

代码:

public partial class MainPage
{
    private readonly string[] _months = new[] {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

    public MainPage()
    {
        InitializeComponent();
        ListBox.ItemsSource = _months;
    }

    private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        PageTitle.Text = (string)ListBox.SelectedItem;
    }
}