4
我做了一個程序,它從文本框中的用戶獲取價值,並將該值添加到ComboBox中,該工作正常,但是當我關閉程序時,它丟失了所有信息因爲我沒有使用數組,但現在我希望它應該動態地將值添加到XML關閉程序後永久存儲在哪裏? plz幫助我,我怎麼能做到這一點將動態值添加到XML中的組合框?
我的C#代碼
namespace PopupDemo
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Show_Click(object sender, RoutedEventArgs e)
{
MyPopup.IsOpen = true;
}
private void Add_Click(object sender, RoutedEventArgs e)
{
comboBox.Items.Add(textbox.Text);
MyPopup.IsOpen = false;
}
private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
}
}
我的XML代碼
<Window x:Class="PopupDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ToolTip Demo"
Height="335.461"
Width="457.092">
<Grid Margin="10">
<Button Click="Show_Click" Margin="82,179,68,30">Show Popup</Button>
<Popup Name="MyPopup"
Placement="Mouse"
HorizontalOffset="-100"
VerticalOffset="-100"
AllowsTransparency="True"
>
<Grid>
<Ellipse Width="300" Height="300" Fill="Aquamarine"/>
<TextBox Width="200" Height="40" Name="textbox" ></TextBox>
<Button Click="Add_Click" Margin="77,209,63,60" Height="50" Width="150" RenderTransformOrigin="0.532,3.873">Add</Button>
</Grid>
</Popup>
<ComboBox x:Name="comboBox" HorizontalAlignment="Left" Margin="82,63,0,0" VerticalAlignment="Top" Width="279" Height="83" SelectionChanged="comboBox_SelectionChanged"/>
</Grid>
</Window>
它給數據源聲明錯誤 – Marina
@Marina我犯了一個錯字,忘了添加'ValueMember'和'DisplayMember'。更新了我的答案。請立即檢查。 – Badiparmagi
您可以加入http://chat.stackoverflow.com/rooms/126005/wpf – Marina