-1

below is my XML generated by using XMLSerializer and there are propertygrid's properties. What I wish to do is to edit the contents in XML file so that the property can be hidden, instead of change the code using [browsable(false)]. For example, there are Name, ID, Mode and email, these 4 properties, and I want to hide then Name by editing the content in XML file. What should I do to achieve this?

<?xml version="1.0" encoding="utf-8" ?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Name xsi:type="xsd:string">Hello</Name>
  <ID xsi:type="xsd:string">1132701760</ID>
  <Mode xsi:type="xsd:string">burst</Mode>
  <Email xsi:type="xsd:string">junxiong1995@hotmail.com</Email>
</Person>
Mers Tho
  • 149
  • 1
  • 9
  • 1
    Please see ["Should questions include “tags” in their titles?"](http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their-titles), where the consensus is "no, they should not"! –  Mar 29 '16 at 07:59
  • I did not understand what you want, what do you mean by "property" in xml ? could you please paste an example to explain ? – Hamza_L Mar 29 '16 at 08:00
  • 1
    XML does not have any inherent knowledge of visiblity on properties/attributes/... are you really talking about XML, or rather XAML? –  Mar 29 '16 at 08:00
  • Possible duplicate of [WPF BooleanToVisibilityConverter that converts to Hidden instead of Collapsed when false?](http://stackoverflow.com/questions/3128023/wpf-booleantovisibilityconverter-that-converts-to-hidden-instead-of-collapsed-wh) – Ghassen Mar 29 '16 at 08:23
  • you have to create a class that inherit from IValueConverter see this link: http://www.wpftutorial.net/ValueConverters.html – Ghassen Mar 29 '16 at 08:26
  • @AndreasNiedermair Hi, I'm talking about XML, because I want to make the edit in XML file, so that I do not need to open the program to edit and save it. so you mean that we can not edit the visibility of property in XML file? I was quite new in programming. sorry if there's stupid question. – Mers Tho Mar 29 '16 at 09:54

1 Answers1

2

MVVM approach:

Basically it implements by built-in class in .Net BooleanToVisibilityConverter:

<UserControl.Resources>
    <BooleanToVisibilityConverter x:Key="booleanVisibilityConverter"/>
</UserControl.Resources>

Let me show work example:

XAML:

<UserControl.Resources>
    <BooleanToVisibilityConverter x:Key="booleanVisibilityConverter"/>
</UserControl.Resources>
...
<Button Content="Hello, I am the button" Visibility="{Binding ShowButton, 
                Converter={StaticResource booleanVisibilityConverter}}"/>

ViewModel:

private bool _showButton = false;
public bool ShowButton
{
   get { 
         //you can write logic here to get values from XML
         return _showButton; 
   }
   set
   {
      if (value != _showButton)
      {
         //you can write logic here to get values from XML
         _showButton = value;
         OnPropertyChanged("ShowButton");
      }
   }
}

If you want to change the Visibility of your Button in code, you can make it by this code in ViewModel:

ShowButton = false;

If you use code-behind approach:

XAML:

<Button Content="Hello, I am the button" Name="btn"/>

Code-behind:

 //read xml file
 if(...your logic here...)
    btn.Visibitity= System.Windows.Visibility.Visible;
StepUp
  • 36,391
  • 15
  • 88
  • 148
  • thank you for answering, Sorry for make u misunderstood on my question. are these possible to make the edit in XML? – Mers Tho Mar 29 '16 at 10:08
  • @junxiong yeah, just read your xml file and use your taken value from xml inside of `ShowButton` property, if you use MVVM approach. If you use code-behind approach, at first you should read values from xml, then write logic whether to show or not your `Controls`. – StepUp Mar 29 '16 at 10:13
  • But I still have to read values first then only write the logic in the code right? because what I wish to do is like we have a boolean value for the property, I'm able to change true or false in XML file, if it is true, the property will be hidden. Something like this. – Mers Tho Mar 29 '16 at 10:21
  • @junxiong in any case you should read values at first from xml, then use logic whether to show `Controls` or not. Otherwise, how can program know about values from xml file? – StepUp Mar 29 '16 at 10:38
  • 1
    alright, I've got it. You solved my doubts, thank you for solving and the codes!! – Mers Tho Mar 29 '16 at 16:32