PDA

Επιστροφή στο Forum : c# combox



ditsikts
20-05-12, 12:50
Πως μπορώ να πάρω το string που είναι επιλεγμένο στο comboxbox;
Προσπαθώ με το

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace UltimateConverter
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class BlankPage : Page
{
public BlankPage()
{
this.InitializeComponent();
}

/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}

private void ComboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//if (ComboBox1.SelectedIndex==0)
if (ComboBox1.SelectedItem.ToString()=="mikos")
{
TextBlock1.Text = "inches se ekatosta";
}
}


}
}

error δε βγάζει, όμως ποτέ η if δε γίνεται true.
Και το gui

<Page
x:Class="UltimateConverter.BlankPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UltimateConverter"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid Background="{StaticResource ApplicationPageBackgroundBrush}">
<TextBox x:Name="textBox1a" HorizontalAlignment="Left" Margin="252,50,0,0" TextWrapping="Wrap" VerticalAlignment="Top"/>
<TextBox x:Name="textBox1b" HorizontalAlignment="Left" Margin="562,50,0,0" TextWrapping="Wrap" VerticalAlignment="Top"/>
<ComboBox x:Name="ComboBox1" HorizontalAlignment="Left" Margin="69,50,0,0" VerticalAlignment="Top" Width="120" SelectionChanged="ComboBox1_SelectionChanged">
<ComboBoxItem Content="mikos"/>
<ComboBoxItem Content="emvadon"/>
</ComboBox>
<TextBlock x:Name="TextBlock1" HorizontalAlignment="Left" Margin="348,50,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="32" Width="200" />

</Grid>
</Page>

Thuglife
21-05-12, 13:25
private void ComboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var item = ComboBox1.SelectedItem as ComboBoxItem;

if (item != null && item.Content.ToString() == "mikos")
TextBlock1.Text = "inches se ekatosta";

}

ditsikts
21-05-12, 13:42
τελικά μου δούλεψε αυτό


ComboBoxItem typeItem = (ComboBoxItem)ComboBox1.SelectedItem;
string value = typeItem.Content.ToString();

Ευχαριστώ για την απάντηση:)

*ίδια λύση έδωσες

@ ADSLgr.com All rights reserved.