Tell me how to pass to the event the path to another EXE file.

There is:

<StackPanel x:Name="stackPanel1" MouseLeftButtonDown="ΠΊΠ½MouseLeftButtonDown"> <Image x:Name="ΠΊΠ½1" Height="64" Margin="0,10,55.8,10" VerticalAlignment="Top" RenderTransformOr Source="рисунки/111.png" HorizontalAlignment="Right" Width="64"/> <Image x:Name="ΠΊΠ½2" Height="64" Margin="0,10,55.8,10" VerticalAlignment="Top" RenderTransformOrigin="0.5,0.5" Source="рисунки/111.png" HorizontalAlignment="Right" Width="64"/> <Image x:Name="ΠΊΠ½3" Height="64" Margin="0,10,55.8,10" VerticalAlignment="Top" RenderTransformOrigin="0.5,0.5" Source="рисунки/111.png" HorizontalAlignment="Right" Width="64"/> <Image x:Name="ΠΊΠ½4" Height="64" Margin="0,10,55.8,10" VerticalAlignment="Top" RenderTransformOrigin="0.5,0.5" Source="рисунки/111.png" HorizontalAlignment="Right" Width="64"/> <Image x:Name="ΠΊΠ½5" Height="64" Margin="0,10,55.8,10" VerticalAlignment="Top" RenderTransformOrigin="0.5,0.5" Source="рисунки/111.png" HorizontalAlignment="Right" Width="64"/> <Image x:Name="ΠΊΠ½6" Height="64" Margin="0,10,55.8,10" VerticalAlignment="Top" RenderTransformOrigin="0.5,0.5" Source="рисунки/111.png" HorizontalAlignment="Right" Width="64"/> <Image x:Name="ΠΊΠ½7" Height="64" Margin="0,10,55.8,10" VerticalAlignment="Top" RenderTransformOrigin="0.5,0.5" Source="рисунки/111.png" HorizontalAlignment="Right" Width="64"/> <Image x:Name="ΠΊΠ½8" Height="64" Margin="0,10,55.8,10" VerticalAlignment="Top" RenderTransformOrigin="0.5,0.5" Source="рисунки/111.png" HorizontalAlignment="Right" Width="64"/> <Image x:Name="ΠΊΠ½9" Height="64" Margin="0,10,55.8,10" VerticalAlignment="Top" RenderTransformOrigin="0.5,0.5" Source="рисунки/111.png" HorizontalAlignment="Right" Width="64"/> <Image x:Name="ΠΊΠ½10" Height="64" Margin="0,10,55.8,10" VerticalAlignment="Top" RenderTransformOr Source="рисунки/111.png" HorizontalAlignment="Right" Width="64"/> </StackPanel> 

The paths for each image button are stored in the Knopki.xml ;

 <?xml version="1.0" standalone="yes"?> <DocumentElement> <Knopki> <nn>ΠΊΠ½1</nn> <НазваниС>xxx1</НазваниС> <Π’ΠΈΠ΄ΠΈΠΌΠΎΡΡ‚ΡŒ>true</Π’ΠΈΠ΄ΠΈΠΌΠΎΡΡ‚ΡŒ> <ΠŸΡƒΡ‚ΡŒ>C:\progi\xxx1.exe</ΠŸΡƒΡ‚ΡŒ> <Иконка>xxx1.png</Иконка> </Knopki> <Knopki> <nn>ΠΊΠ½2</nn> <НазваниС>xxx2</НазваниС> <Π’ΠΈΠ΄ΠΈΠΌΠΎΡΡ‚ΡŒ>true</Π’ΠΈΠ΄ΠΈΠΌΠΎΡΡ‚ΡŒ> <ΠŸΡƒΡ‚ΡŒ>C:\progi\xxx2.exe</ΠŸΡƒΡ‚ΡŒ> <Иконка>xxx2.png</Иконка> </Knopki> <Knopki> <nn>ΠΊΠ½3</nn> <НазваниС>xxx3</НазваниС> <Π’ΠΈΠ΄ΠΈΠΌΠΎΡΡ‚ΡŒ>true</Π’ΠΈΠ΄ΠΈΠΌΠΎΡΡ‚ΡŒ> <ΠŸΡƒΡ‚ΡŒ>C:\progi\xxx3.exe</ΠŸΡƒΡ‚ΡŒ> <Иконка>xxx3.png</Иконка> </Knopki> <Knopki> <nn>ΠΊΠ½4</nn> <НазваниС>xxx4</НазваниС> <Π’ΠΈΠ΄ΠΈΠΌΠΎΡΡ‚ΡŒ>true</Π’ΠΈΠ΄ΠΈΠΌΠΎΡΡ‚ΡŒ> <ΠŸΡƒΡ‚ΡŒ>C:\progi\xxx4.exe</ΠŸΡƒΡ‚ΡŒ> <Иконка>xxx4.png</Иконка> </Knopki> <Knopki> <nn>ΠΊΠ½5</nn> <НазваниС>xxx</НазваниС> <Π’ΠΈΠ΄ΠΈΠΌΠΎΡΡ‚ΡŒ>true</Π’ΠΈΠ΄ΠΈΠΌΠΎΡΡ‚ΡŒ> <ΠŸΡƒΡ‚ΡŒ>C:\progi\xxx5.exe</ΠŸΡƒΡ‚ΡŒ> <Иконка>xxx5.png</Иконка> </Knopki> </DocumentElement> 

How to click on the Image to transfer here the path to the EXE-file?

 private void MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { System.Diagnostics.Process.Start( ??? ); } 

When clicking on Image necessary to run different EXE files for each Image , specified in the XML file.

Changed. Made it so - changed MouseButtonEventArgs to RoutedEventArgs :

 private void ΠΊΠ½MouseLeftButtonDown(object sender, RoutedEventArgs e) { XDocument XMLdoc = XDocument.Load("Knopki.xml" ); foreach (XElement kE in XMLdoc.Element("DocumentElement").Elements("Knopki")) { XElement nK = kE.Element("nn"); XElement patch = kE.Element("ΠŸΡƒΡ‚ΡŒ"); if (nK.Value.ToString() == ?????? ) { richTextBox1.AppendText("\n" + nK.Value.ToString() + " " + patch.Value.ToString()); System.Diagnostics.Process.Start( patch ); } } } 

    2 answers 2

    Your task falls perfectly on MVVM.

    For starters, VM. You need to create a class that describes a single object. Somehow:

     class Entry { public string Title { get; } public bool IsVisible { get; } public string ExecutablePath { get; } public string IconPath { get; } } 

    In your model, you must read the XML, turn each of the Knopki tags into an instance of Entry , and put all the Entry into a collection (at least in a List<Entry> ).

    Now, View. Everything is simple with him. As you display a list, you need an ItemsControl . For the display of a single item is responsible, of course, ItemTemplate .

     <ItemsControl ItemsSource="{Binding EntryCollection}"> <ItemsControl.ItemTemplate> <DataTemplate DataType="{x:Type vm:Entry}"> <Button> <Image Height="64" Width="64" Source="{Binding IconPath}" /> </Button> <DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> 

    What we still do not have is the reaction of buttons to pressing. For this you need to pick up a team to them. Take any implementation of RelayCommand (for example, from here ), pass the launch path as a parameter. Total:

     <ItemsControl Name="MainContainer" ItemsSource="{Binding EntryCollection}"> 

    and

     <Button Command="{Binding DataContext.InvokeExecute, ElementName=MainContainer}" CommandParameter="{Binding ExecutablePath}"> 

    Well, you need the command itself in the main VM:

     ICommand InvokeExecute = new RelayCommand<string>(path => Process.Start(path)); 
    • VladD, thanks for the answer, but, 'Your task falls perfectly on MVVM.' - vova-forum
    • VladD, thanks for the reply, but maybe there is some way to bind to each Image your own path from the XML file - vova-forum
    • You can try XmlDataProvider to use xml instead of models. Msdn - AvtPhenix
    • Made changes to the knMouseLeftButtonDown method (see last item in question) now it remains to get the name Image I try via e.OriginalSource - vova-forum
    • one
      @ vova-forum: You are trying to write on WPF like on WinForms. This is also possible, but the quality of the code is worse. Write program logic in a click handler as if it were an antipattern. - VladD

    I answer my question, did like this, it works

     private void ΠΊΠ½MouseLeftButtonDown(object sender, RoutedEventArgs e) { XDocument XMLdoc = XDocument.Load("Knopki.xml" ); foreach (XElement kE in XMLdoc.Element("DocumentElement").Elements("Knopki")) { XElement nK = kE.Element("nn"); XElement patch = kE.Element("ΠŸΡƒΡ‚ΡŒ") ; if (nK.Value.ToString() == ((Image)e.OriginalSource).Name) System.Diagnostics.Process.Start(patch.Value.ToString()); } } 
    • If someone tells you how to perform more optimally, for example, in one line - vova-forum
    • 2
      Optimally, it is to throw away what you have now and listen to the advice from @VladD - Ev_Hyper