0

I am trying to get an XML element in a textbox from the users choosen combox element also from the same XML file.

I am using WPF, i am able to populate the combobox with the elements from the xml file using the following code

 <ComboBox  Grid.Column="1" Height="21"  HorizontalAlignment="Left" Margin="0,32,0,0" Name="QueryChooser" VerticalAlignment="Top" Width="189" ItemsSource="{Binding}" SelectionChanged="QueryChooser_SelectionChanged" />

My xaml.cs

 private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Queryslistload();
        }

        private void Queryslistload()
        {
            var xElem = XElement.Load(@"Querys.xml");

            var querys = from query in xElem.Descendants("QueryLay")
                         orderby query.Element("QueryName").Value
                         select query.Element("QueryName").Value;
            QueryChooser.ItemsSource = querys;

        }

this is my xml file itself

<?xml version="1.0" encoding="utf-8"  standalone="yes" ?> 
<Querys>
    <QueryLay>
    <QueryID>
        1
    </QueryID>
    <QueryName>Check Logspace</QueryName>
    <Query>dbcc sqlperf(logspace)</Query>
    </QueryLay>
    <QueryLay>
    <QueryID>
        2
    </QueryID>
    <QueryName>Check Spaceused</QueryName>
    <Query>sp_spaceused</Query>
    </QueryLay>

    </Querys>

so now if the user selects the check logspace from combobox i want the query element to be displayed in the textbox

how do i achieve this?

UPDATED

public class Query
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Value { get; set; }
        }
        private void QueryChooser_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var xElem = XElement.Load(@"Querys.xml");
            var querys =  xElem.Descendants("QueryLay").Select( e => 
                new Query{
                         Id = Convert.ToInt32(e.Element("QueryID").Value),
       Name = e.Element("QueryName").Value,
       Value = e.Element("Query").Value
                         }).OrderBy(q=>q.Name)
                         select query.Element("QueryName").Value ;

            listBox1.ItemsSource = querys;
        }
H.B.
  • 166,899
  • 29
  • 327
  • 400
JackyBoi
  • 2,695
  • 12
  • 42
  • 74

1 Answers1

1

Rather than bind the ComboBox directly to the XElement's returned from your query, you should create your own type which represents a QueryName/Query combination, and then define a LINQ-to-XML query which projects the elements to a sequence of this type.

You can then bind the ComboBox SelectedItem to a property on your data context.

E.g:

Query Type

public class Query
{
   public int Id { get; set; }
   public string Name { get; set; }
   public string Value { get; set; }
}

Data Context

var xElem = XElement.Load(@"Querys.xml");

this.Queries = xElem.Descendants("QueryLay").Select(e =>
     new Query 
     {
       Id = Convert.ToInt32(e.Element("QueryID").Value),
       Name = e.Element("QueryName").Value,
       Value = e.Element("Query").Value
     }).OrderBy(q => q.Name);

public Query SelectedQuery { get; set; }

View

<ComboBox ItemsSource="{Binding Queries}" 
    SelectedItem="{Binding SelectedQuery}" 
    DisplayMemberPath="Name"
    ... />

this.SelectedQuery.Value in your data context will then give you the selected query value.

devdigital
  • 34,151
  • 9
  • 98
  • 120
  • I modified accordingly and getting a few error is this is what you meant? (please see the original question updated at the end of the question – JackyBoi Mar 11 '12 at 15:32
  • No, sorry, the query that reads the XML should only happen once on initialisation, so that code should not go in the selection changed handler. Also, you don't need select query.Element("QueryName").Value ; at the end of the query. – devdigital Mar 11 '12 at 15:35
  • I am bit confused as to where does your Datacontext code goes into? is it the querylistload method? – JackyBoi Mar 11 '12 at 15:38
  • Yes. It's showing both the code to use to query the XML and produce your collection of queries to bind to, and also a property to place on your class which will store the selected query, and is bound in the combobox too. – devdigital Mar 11 '12 at 15:40
  • Note that this.Queries in this case is another property on your class which is your collection of queries (for example IEnumerable) – devdigital Mar 11 '12 at 15:40
  • so in the whole code i donot see as in where does a textbox text gets binded to the item that i have choosen in the combobox.. possible to continue this in chat? – JackyBoi Mar 11 '12 at 15:41
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/8758/discussion-between-jackyboi-and-devdigital) – JackyBoi Mar 11 '12 at 15:42
  • Hi, i am trying to copy the textvalue from the textblock to clipboard y is that i am not able to declare the the element in the C# code?cause it got to do something with the text property being binded to the combobox? – JackyBoi Mar 12 '12 at 03:56
  • It would be better to start a new question with the specific problem. – devdigital Mar 12 '12 at 12:58
  • ok here is the new question http://stackoverflow.com/questions/9669303/unable-to-copy-text-to-clipboard-while-binded – JackyBoi Mar 12 '12 at 14:49