ComboBox在TXT中写单词

时间:2016-10-13 14:03:40

标签: c# wpf combobox

让我告诉你我的代码中包含的内容:

public List<String> listQuality = new List<string>();
public int qualityChoose;

InitializeComponent();

listQuality.Add("Fastest");
listQuality.Add("Fast");
listQuality.Add("Simple");
listQuality.Add("Good");
listQuality.Add("Beautiful");
listQuality.Add("Fantastic");

foreach (String item in listQuality)
{
    listQualityy.Items.Add(item);
}

string textWriteQuality;
textWriteQuality = "-screen-quality " + qualityChoose + Environment.NewLine;

File.AppendAllText(@"./arguments.txt", textWriteQuality);

但是它不起作用,我认为问题是&#34; init&#34;但我不确定我是不是在编码它是我的第一个节目(是的,我再说一遍xd)

我已经跳过了其他一些对问题毫无用处的代码 - 我认为 -

1 个答案:

答案 0 :(得分:0)

你的问题不是很容易理解,就像你可以做这样的例子一样

XAML:

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <ComboBox Grid.Row="0" ItemsSource="{Binding listQuality}" SelectedItem="{Binding qualityChoose}">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Item2}"/>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>

        <Button Grid.Row="1" Content="Save" Click="Button_Click"/>

    </Grid>

代码隐藏:

public List<Tuple<int, String>> listQuality { get; set; }
        public Tuple<int, String> qualityChoose { get; set; }

        public MainWindow()
        {
            InitializeComponent();
            listQuality = new List<Tuple<int, string>>();
            listQuality.Add(new Tuple<int, string>(0, "Fastest"));
            listQuality.Add(new Tuple<int, string>(1, "Fast"));
            listQuality.Add(new Tuple<int, string>(2, "Simple"));
            listQuality.Add(new Tuple<int, string>(3, "Good"));
            listQuality.Add(new Tuple<int, string>(4, "Beautiful"));
            listQuality.Add(new Tuple<int, string>(5, "Fantastic"));

            this.DataContext = this;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            int quality = qualityChoose.Item1;
        }

当您点击 qualityChoose.Item1 中的“保存”按钮时,您的值为

修改

使用元组,你可以使用List&lt;字符串&gt;在您的问题中,像这样修改xaml:

...
<ComboBox Grid.Row="0" ItemsSource="{Binding listQuality}" SelectedIndex="{Binding selIndex}">
...

并在codebehind中添加“selIndex”属性:

public int selIndex { get; set; }

在这个属性中有所选字符串的索引。 这更容易,但你无法决定品质的价值。