How to bind panels with controls in devexpress docklayoutmanager - c#

File Name SampleProject
My xaml file Mainwindow.xaml----------
<Window x:Class="SampleProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dxdo="http://schemas.devexpress.com/winfx/2008/xaml/docking" Closing="Window_Closing"
Title="MainWindow" Height="800" Width="800" WindowState="Maximized">
<Grid>
<dxdo:DockLayoutManager x:Name="docklayoutmanager1" dxdo:RestoreLayoutOptions.RemoveOldPanels="False" dxdo:RestoreLayoutOptions.RemoveOldLayoutControlItems="False" Margin="0,50,0,0">
</dxdo:DockLayoutManager>
<Button Width="100" VerticalAlignment="Top" HorizontalAlignment="Center" Height="30" x:Name="ClickMe" Content="Click Me" Click="ClickMe_Click" Margin="323,10,369,729" />
</Grid>
</Window>
`
My xaml.cs file MainWindow.xaml.cs-------------------
using DevExpress.Xpf.Docking;
using DevExpress.Xpf.Layout.Core;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace SampleProject
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
if(File.Exists("test.xml"))
{
docklayoutmanager1.RestoreLayoutFromXml("test.xml");
}
}
private void ClickMe_Click(object sender, RoutedEventArgs e)
{
var panel = docklayoutmanager1.DockController.AddPanel(DockType.None);
panel.Caption = "Test Panel";
panel.Name = "myPanel";
panel.Content = "I want to serialize this code using guid Please provide example for creating such binding.";
FloatGroup floatgroup = new FloatGroup();
floatgroup.Name = "myFloatGroup";
floatgroup.FloatSize = new Size(500,500);
floatgroup.FloatLocation = new Point(300, 300);
floatgroup.Add(panel);
docklayoutmanager1.FloatGroups.Add(floatgroup);
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
string xmlFilePath = System.AppDomain.CurrentDomain.BaseDirectory + "test.xml";
if (File.Exists("test.xml"))
{
docklayoutmanager1.SaveLayoutToXml("test.xml");
}
else
{
using (File.Create("test.xml")) { }
docklayoutmanager1.SaveLayoutToXml("test.xml");
}
}
}
}
I have a document layout manager programmatically getting panels and controls which is having panels and controls in them.
While saving, I am achieving this with SaveLayoutToXml() for my panels and Serializing controls using bformatter.Serialize();.
since i have lot of panels and unique controls in them. I want to get the same controls back in same panels as it was before saving and serializing. Please provide me with a code to identify with a unique id for both panels and controls.
And do I have any integer id to which I can assign GUID as BindableName does not work with it.
Thanks
Desh

Create your panels programmatically and and them to your xaml file at run time.
UserC = new UserC();
DocumentPanel dpanel = new DocumentPanel();
dpanel.Name = "OpenUC" + count;
dpanel.Caption = "Open User Control";
dpanel.BindableName = "OpenUC" + count;
var content = UserC;
dpanel.Content = content;
FloatGroup fGroup = new FloatGroup();
fGroup.FloatSize = new Size(500, 500);
fGroup.Items.Add(dpanel);
DockLayoutManager1.FloatGroups.Add(fGroup);

This is how to add a User Control, since nobody seems to be interested in helping, i searched and done something like this:
private FloatGroup CreateDocPanelFloatGroup(DocumentPanel dp)
{
FloatGroup floatGroup = new FloatGroup();
floatGroup.FloatSize = new Size(500, 500);
floatGroup.Items.Add(dp);
return floatGroup;
}
private void OpenUC_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e)
{
openUC = new OpenUC();
DocumentPanel dp = new DocumentPanel();
dp.Name = "OpenUC" + count;
dp.Caption = "Open User Control";
dp.BindableName = "OpenUC" + count;
var content = openUC;
dp.Content = content;
DockLayoutManager1.FloatGroups.Add(CreateDocPanelFloatGroup(dp));
}

Related

C# "Controls" Does Not Exist in The Current Context

Error
In my application I'm trying to use "Controls.Add", but Visual Studio keeps giving me the error:
"Error 1 The name 'Controls' does not exist in the current context
C:\Users\Admin\Desktop\Demo\Demo\MainWindow.xaml.cs 42 13 Demo"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Demo
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Window1 window = new Window1();
Button button = new Button();
window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
button.Content = "New Button";
button.Visibility = Visibility.Visible;
button.Height = 50;
button.Width = 200;
Controls.add(button); <-- ERROR IS FOUND HERE
window.Show();
}
}
}
I'm using WPF, coming from WinForms. Any advice is appreciated, thanks.
You need to do something along the lines of:
var window = new Window1();
var stackPanel = new StackPanel { Orientation = Orientation.Vertical };
Button button = new Button();
button.Content = "New Button";
button.Visibility = Visibility.Visible;
button.Height = 50;
button.Width = 200;
stackPanel.Children.Add(button);
window.Content = stackPanel;
window.Show();
Although, I'd recommend defining all UI components in the XAML and reading up on the MVVM pattern.
<StackPanel Name=“splMain“>
<Button Name=“btnAddMore“ Click=“btnAddMore_Click“>Add Another</Button>
Also on button click :
System.Windows.Controls.Button newBtn = new Button();
newBtn.Content = “A New Button”;
splMain.Children.Add(newBtn);
Button SaveButton = new Button();
SaveButton.Name = "Save";
SaveButton.Content = "Save";
Grid1.Children.Add(SaveButton);

Dynamic Generation of Rows in WPF MVVM

I am creating a Data Entry Form having a ComboBox & Few TextBoxes in First Row. I want a new Row with same Elements to be Generated on Pressing Enter Key from Last TextBox in First Row.
How do i achieve it? Is there any Data Template or Item Template or anything as such that can work for this Data Entry Form.
I also read about Observable Collection Class & IEnumerable, can this help me achieving? How?
I am Newbie to WPF & MVVM, have done a lot of research but couldn't find the exact solution.
I have already done this,
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="500" Width="420">
<Grid Name="_mainGrid" HorizontalAlignment="Left">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="227"/>
<ColumnDefinition Width="77" />
<ColumnDefinition Width="77" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="27"/>
</Grid.RowDefinitions>
</Grid>
</Window>
Also my .CS Code is as follows
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using System.Reflection;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
int rowIndex, Dr = 0, Cr = 0;
TextBox PTextBox, DrAmtTextBox, CrAmtTextBox;
ComboBox Combobox;
List<TextBox> txtboxParticulars = new List<TextBox>();
List<TextBox> txtboxDrAmount = new List<TextBox>();
List<TextBox> txtboxCrAmount = new List<TextBox>();
List<ComboBox> cmbboxDrCr = new List<ComboBox>();
public MainWindow()
{
InitializeComponent();
CreateRow();
}
public void CreateRow()
{
RowDefinition newRow = new RowDefinition();
newRow.Height = new GridLength(30, GridUnitType.Pixel);
_mainGrid.RowDefinitions.Insert(_mainGrid.RowDefinitions.Count - 1, newRow);
rowIndex = _mainGrid.RowDefinitions.Count - 2;
AddComboBox();
AddPTextBox();
AddDrCrTextBox();
}
public void AddComboBox()
{
Combobox = new ComboBox();
Combobox.Items.Add("Dr.");
Combobox.Items.Add("Cr.");
Combobox.SelectedItem = "Dr.";
Combobox.Height = 25;
Combobox.Name = "DrCr" + rowIndex.ToString();
Combobox.SelectionChanged += new SelectionChangedEventHandler(Combobox_SelectionChanged);
Combobox.KeyUp += new KeyEventHandler(Combobox_KeyUp);
Grid.SetRow(Combobox, rowIndex);
Grid.SetColumn(Combobox, 0);
_mainGrid.Children.Add(Combobox);
cmbboxDrCr.Add(new ComboBox { Text = Combobox.Name });
cmbboxDrCr[rowIndex].Text = "Dr.";
Combobox.Focus();
}
void Combobox_KeyUp(object sender, KeyEventArgs e)
{
if (Combobox.SelectedItem.ToString() == "Dr.")
{
cmbboxDrCr[rowIndex].Text = "Dr.";
DrAmtTextBox.Visibility = System.Windows.Visibility.Visible;
CrAmtTextBox.Visibility = System.Windows.Visibility.Hidden;
}
else
{
cmbboxDrCr[rowIndex].Text = "Cr.";
DrAmtTextBox.Visibility = System.Windows.Visibility.Hidden;
CrAmtTextBox.Visibility = System.Windows.Visibility.Visible;
}
}
void Combobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (Combobox.SelectedItem.ToString() == "Dr.")
{
cmbboxDrCr[rowIndex].Text = "Dr.";
DrAmtTextBox.Visibility = System.Windows.Visibility.Visible;
CrAmtTextBox.Visibility = System.Windows.Visibility.Hidden;
PTextBox.Focus();
}
else
{
cmbboxDrCr[rowIndex].Text = "Cr.";
DrAmtTextBox.Visibility = System.Windows.Visibility.Hidden;
CrAmtTextBox.Visibility = System.Windows.Visibility.Visible;
PTextBox.Focus();
}
}
public void AddPTextBox()
{
PTextBox = new TextBox();
PTextBox.Name = "Particulars" + rowIndex.ToString();
PTextBox.Height = 25;
PTextBox.Width = 225;
Grid.SetRow(PTextBox, rowIndex);
Grid.SetColumn(PTextBox, 1);
_mainGrid.Children.Add(PTextBox);
txtboxParticulars.Add(new TextBox { Text = PTextBox.Name });
}
public void AddDrCrTextBox()
{
DrAmtTextBox = new TextBox();
DrAmtTextBox.Name = "Dr" + rowIndex.ToString();
DrAmtTextBox.Height = 25;
DrAmtTextBox.Width = 75;
DrAmtTextBox.KeyDown += new KeyEventHandler(DrCrAmtTextBox_KeyDown);
Grid.SetRow(DrAmtTextBox, rowIndex);
Grid.SetColumn(DrAmtTextBox, 2);
_mainGrid.Children.Add(DrAmtTextBox);
txtboxDrAmount.Add(new TextBox { Text = DrAmtTextBox.Name });
CrAmtTextBox = new TextBox();
CrAmtTextBox.Name = "Cr" + rowIndex.ToString();
CrAmtTextBox.Height = 25;
CrAmtTextBox.Width = 75;
CrAmtTextBox.Visibility = System.Windows.Visibility.Hidden;
CrAmtTextBox.KeyDown += new KeyEventHandler(DrCrAmtTextBox_KeyDown);
Grid.SetRow(CrAmtTextBox, rowIndex);
Grid.SetColumn(CrAmtTextBox, 3);
_mainGrid.Children.Add(CrAmtTextBox);
txtboxCrAmount.Add(new TextBox { Text = DrAmtTextBox.Name });
}
void DrCrAmtTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
AddDataToList();
CountDrCr();
CreateRow();
}
}
void CountDrCr()
{
for (int i = 0; i <= rowIndex; i++)
{
if (cmbboxDrCr[i].Text == "Dr.")
{
Dr++;
}
else
{
Cr++;
}
}
}
void AddDataToList()
{
cmbboxDrCr[rowIndex].Text = Combobox.Text;
txtboxParticulars[rowIndex].Text = PTextBox.Text;
txtboxDrAmount[rowIndex].Text = CrAmtTextBox.Text;
txtboxCrAmount[rowIndex].Text = DrAmtTextBox.Text;
MessageBox.Show(cmbboxDrCr[rowIndex].Text +" "+ txtboxParticulars[rowIndex].Text + " " + txtboxDrAmount[rowIndex].Text + " " + txtboxCrAmount[rowIndex].Text);
}
}
}
I know this is not the Right way, so please help me with WPF MVVM method of doing this!!!
After going through the Answers here i have done the Following
Created a DataType
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DataTemplate
{
class YourDataType
{
public string DrCr { get; set; }
public string Particulars { get; set; }
public float Amount { get; set; }
public string Narration { get; set; }
}
}
MainWindow.Xaml File
<Window x:Class="DataTemplate.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DataTemplate"
Title="MainWindow" Height="350" Width="750">
<Grid>
<ListBox ItemsSource="{Binding YourCollection}">
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type local:YourDataType}">
<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Horizontal">
<ComboBox SelectedIndex="0">
<ComboBoxItem Content="Dr."/>
<ComboBoxItem Content="Cr."/>
</ComboBox>
<TextBox Width="350"/>
<TextBox Width="150"/>
</StackPanel>
<Label Content="Narration"/>
<TextBox Width="450"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
MainWindow.CS File
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
namespace DataTemplate
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private ObservableCollection<YourDataType> YourCollection;
public MainWindow()
{
InitializeComponent();
YourCollection.Add(new YourDataType());
}
}
}
While it may seem natural for a non WPF developer to build their UI in code, that is generally not how it is done in WPF. Learn to do it in XAML and you will save yourself many headaches.
First create a data type, let's call it YourDataType for example, that contains all of the required properties for each 'row'. Then declare an ObservableCollection<YourDataType> property to data bind to a collection control, let's call it YourCollection. Finally, declare a DataTemplate that defines what each row should look like:
<ListBox ItemsSource="{Binding YourCollection}">
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type YourPrefix:YourDataType}">
<!-- Define your 'row' controls here -->
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Now to add a new empty row, all you need to do is to add a new item to your data bound collection:
YourCollection.Add(new YourDataType());
For further information, please refer to the Data Binding Overview page on MSDN.
You can create a WPF User Control with the complete structure inside (the comboBoxand and the textboxes), you can create a single list of this usercontrol
for example you can create a FormRow.xaml user control similar to this one....
<UserControl x:Class="WPFTEST.FormRow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup- compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
Width="500" Height="26">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<ComboBox Margin="0"/>
<TextBox TextWrapping="Wrap" Text="TextBox" Grid.Column="1" Margin="0"/>
<TextBox TextWrapping="Wrap" Text="TextBox" Grid.Column="2" Margin="0"/>
<TextBox TextWrapping="Wrap" Text="TextBox" Grid.Column="3" Margin="0"/>
</Grid>
</UserControl>
And you can add events and control the model object information from the codebehind.....
in the main window you can have a listView or a Stack full of instances of this single control each instance is a view to a model object and preserves the acess hierarchy....

Grid Lines wont show

Grid lines wont show on runt-time I can seem to see where I am going wrong. This is a WPF window application. I just want to generate columns and rows using C# code. Any ideas why this is not working?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace generator
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void CreateGrid()
{
ColumnDefinition myColum =new ColumnDefinition();
RowDefinition myRow = new RowDefinition();
ColumnDefinition myColum1 = new ColumnDefinition();
RowDefinition myRow1 = new RowDefinition();
Grid myGrid = new Grid();
myRow.Height = new GridLength(1, GridUnitType.Star);
myColum.Width = new GridLength(1, GridUnitType.Star);
myRow1.Height = new GridLength(1, GridUnitType.Star);
myColum1.Width = new GridLength(1, GridUnitType.Star);
myGrid.ColumnDefinitions.Add(myColum);
myGrid.RowDefinitions.Add(myRow);
myGrid.ColumnDefinitions.Add(myColum1);
myGrid.RowDefinitions.Add(myRow1);
myGrid.ShowGridLines = true;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
CreateGrid();
}
}
}
The question may as well read "Grid won't show".
You are creating a new Grid instance, but you never add it to the visual tree. You may assign it to the Window's Content property:
private void CreateGrid()
{
...
Content = myGrid;
}

Background image is changed to default on wp7

In my windows phone 7 application, i have created a button to change the default background image and the users can have their own custom skins on the app. It worked perfectly. But when the users exits the app and relaunches it again the application's background image is changed to default. But what i needed is, the application should have the last image that is chosen by the user even is launched no. of times. Can anyone help me with this? Thanks in advance for your hard work!
private void BackgroundBrowserIcon_MouseEnter(object sender, MouseEventArgs e)
{
var PhotoChooser = new PhotoChooserTask();
PhotoChooser.Completed += new EventHandler<PhotoResult>(PhotoChooser_Completed);
PhotoChooser.Show();
}
void PhotoChooser_Completed(object sender, PhotoResult e)
{
{
if (e.TaskResult == TaskResult.OK)
{
System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
bmp.SetSource(e.ChosenPhoto);
var app = Application.Current as App;
if (app == null)
return;
var imageBrush = new ImageBrush { ImageSource = bmp, Opacity = 1.0d };
this.LayoutRoot.Background = imageBrush;
app.backchanged = true;
app.appbrush = imageBrush;
}
}
}
Are you using IsolatedStorageSettings to store the last chosen photo, and then loading it on app launch?
Update:
Check out this post, as it explains how to code exactly what you are looking to accomplish: http://www.windowsphonegeek.com/tips/All-about-WP7-Isolated-Storage---Read-and-Save-Captured-Image
Check out this article to learn how to store IsolatedStorageSettings.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
namespace F5debugWp7IsolatedStorage
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void button3_Click(object sender, RoutedEventArgs e)
{
IsolatedStorageSettings ISSetting = IsolatedStorageSettings.ApplicationSettings;
if (!ISSetting.Contains("DataKey"))
{
ISSetting.Add("DataKey", txtSaveData.Text);
}
else
{
ISSetting["DataKey"] = txtSaveData.Text;
}
ISSetting.Save();
}
}
}

C# xmldataprovider insert node

I am trying to write an editor for an xml config file. I have the xml file bound to a listview and selecting an element and then clicking edit allows you to edit an element. When the user clicks save, a delegate is called which updates the element within the xml document. I have tried to do the updating in various ways including appendnode, prependnode, insertafter, and replace child. I have gotten them all working without any compile time or runtime errors, yet they don't update the xmldataprovider or the listview. It's probably just something simple that I am missing but I am having trouble figuring out what it is. Can someone please help?
Thanks,
Brian
If it helps, here's the source code of my main form (the one with the listview)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using System.IO;
namespace g2config
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public XmlDataProvider dp;
string cfgfile;
public managecmpnts manage_components;
public delegate void managecmpnts(int id, XmlElement component);
public MainWindow()
{
InitializeComponent();
dp = this.FindResource("configfile") as XmlDataProvider;
cfgfile = "C:\\Users\\briansvgs\\Desktop\\g2config\\g2config\\bin\\Debug\\g2config.pxml";
if(Environment.GetCommandLineArgs().Count() > 1)
{
string path = Environment.GetCommandLineArgs().ElementAt(1);
//MessageBox.Show("Path: " + path);
cfgfile = path;
}
if (File.Exists(cfgfile))
{
dp.Source = new Uri(cfgfile, UriKind.RelativeOrAbsolute);
}
else
{
MessageBox.Show("config file not found");
}
this.Title = this.Title + " (" + cfgfile + ") ";
}
public void browsedir( object sender, EventArgs e)
{
System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
ofd.Filter = "PXML Files (*.pxml)|*.pxml"; ;
//http://www.kirupa.com/net/using_open_file_dialog_pg4.htm
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) //how to browse dirs instead of files?????
{
startpath.Text = ofd.FileName;
}
}
public void newcomponent(object sender, RoutedEventArgs e)
{
componentsettings new_cmpnt = new componentsettings();
new_cmpnt.Title = "Add Component";
new_cmpnt.ShowDialog();
}
public void editcomponent(object sender, RoutedEventArgs e)
{
int selected = components.SelectedIndex;
XmlElement current = (XmlElement)components.Items.CurrentItem;
componentsettings edit_cmpnt = new componentsettings(current);
edit_cmpnt.cmpnt_mgr.manage_components += new manager.mngcmpnts(test);
edit_cmpnt.Title = "Edit Component";
edit_cmpnt.ShowDialog();
}
private void browsedir(object sender, RoutedEventArgs e)
{
System.Windows.Forms.FolderBrowserDialog directory;
directory = new System.Windows.Forms.FolderBrowserDialog();
directory.Description = "Please select the folder containing your g2 configuration (pxml) files. Default is C:\\Program Files\\Exacom, Inc.";
directory.ShowDialog();
startpath.Text = directory.SelectedPath;
}
private void save(object sender, RoutedEventArgs e)
{
MessageBox.Show(dp.Source.ToString());
if(File.Exists(cfgfile + ".old"))
{
File.Delete(cfgfile + ".old");
}
File.Move(cfgfile, cfgfile + ".old");
dp.Document.Save(cfgfile);
}
private void remove(object sender, RoutedEventArgs e)
{
XmlNode component = dp.Document.DocumentElement["Components"].ChildNodes[components.SelectedIndex];
dp.Document.DocumentElement["Components"].RemoveChild(component);
}
private void temp(object sender, RoutedEventArgs e)
{
MessageBox.Show(dp.Document.DocumentElement["Components"].ChildNodes[components.SelectedIndex].InnerText.ToString()); //will this give me trouble????? Probably not since it's my config, but...
}
private void test(int id, XmlElement testelement)
{
//dp.Document.DocumentElement["Components"].ChildNodes[id] = testelement;
//XmlNode testnode = dp.Document.DocumentElement["Components"].ChildNodes[id + 1];
//XmlNode testnode = testelement;
MessageBox.Show(testelement.OuterXml.ToString());
//dp.Document.DocumentElement["Components"].InsertAfter(dp.Document.DocumentElement["Components"].ChildNodes[0], testelement);
//dp.Document.DocumentElement["Components"].RemoveChild(testelement);
//dp.Document.DocumentElement["Components"].ReplaceChild(testnode, dp.Document.DocumentElement["Components"].ChildNodes[id]);
dp.Document.DocumentElement["Components"].PrependChild(testelement);
//dp.Document.NodeChanged();
//dp.Document.DocumentElement["Components"].CloneNode(true);
dp.Document.DocumentElement["Components"].AppendChild(testelement);
//dp.Document.
//MessageBox.Show(dp.Document.OuterXml.ToString());
//MessageBox.Show(dp.Document.DocumentElement["Components"].FirstChild.AppendChild(testelement).OuterXml.ToString());
}
}
}
JMSA. Thank you for the suggestion. I was playing around with it today and I found a solution. It's a little messy, but it works. I copy one of the present nodes and then clear all of the values. Here is the code if anyone is interested.
private void add(object sender, RoutedEventArgs e)
{
System.Xml.XmlNode test = configfile.Document.DocumentElement["Components"].FirstChild;
System.Xml.XmlNode testclone = test.Clone();
for (int i = 0; i < testclone.ChildNodes.Count; i++)
{
testclone.ChildNodes[i].RemoveAll();
}
configfile.Document.DocumentElement["Components"].AppendChild(testclone);
components.SelectedItem = components.Items.Count + 1;
}
My suggestion would be to create a class for each element by using a composite pattern, lazy loading the XML doc in the class and perform operations as you need. Then again save it. Quite a handful though.

Categories