WPF uses a value converter to convert bound data ivalueconverter

Source: Internet
Author: User

Sometimes, we can use a value converter to display the bound data in another format or convert it to another type.
For example, if the path "C: \ ABC \ abc.exe133" of a file is saved in my data, I want to display it as cmdabc.exe in the foreground ".
First, we create a class for the ivalueconverter interface.

  1. ClassGetfilename: ivalueconverter
  2. {
  3. // The convert method is used to convert data into the desired display format.
  4. Public ObjectConvert (ObjectValue, type targettype,ObjectParameter, cultureinfo Culture)
  5. {
  6. Fileinfo Fi =NewFileinfo ((String) Value );
  7. ReturnFi. Name;
  8. }
  9. // The convertback method converts the displayed value to the original format. Because I do not need reverse conversion, an exception is thrown directly.
  10. Public ObjectConvertback (ObjectValue, type targettype,ObjectParameter, cultureinfo Culture)
  11. {
  12. Throw NewNotimplementedexception ();
  13. }
  14. }

To use this converter, We need to map the project namespace to The XAML. For example, if my project name is automatically updated, use local as the space name prefix.

Xmlns: Local = "CLR-namespace: automatic update"

Xmlns: Local = "CLR-namespace: automatic update"
For ease of use, we create a converter object in the resources set.

  1. <Window. Resources>
  2. <Local: getfilenameX: Key = "getfilename"> </Local: getfilename>
  3. </Window. Resources>

<Window. Resources> <br/> <local: getfilename X: Key = "getfilename"> </local: getfilename> <br/> </window. Resources>
Now we use staticresource to point to the converter where data is bound.
<Textblock>

  1. <Textblock. Text>
  2. <BindingPath = "FILENAME">
  3. <Binding. converter>
  4. <Local: getfilename> </local: getfilename>
  5. </Binding. converter>
  6. </Binding>
  7. </Textblock. Text>
  8. </Textblock>

Original article: http://blog.csdn.net/slzlren/article/details/6595905

 

For example:

 
Namespace converter {public class oppconverter: ivalueconverter {# region ivalueconverter member object ivalueconverter. Convert (object value, type targettype, object parameter, system. Globalization. cultureinfo culture) {If (targettype! = Typeof (bool) {Throw new invalidoperationexception ("the target must be a Boolean");} return! (Bool) value;} object ivalueconverter. convertback (object value, type targettype, object parameter, system. Globalization. cultureinfo culture) {Throw new notimplementedexception () ;}# endregion }}
The following is the XAML code:
   <Grid>        <Grid.Resources>            <wf:OppConverter x:Key="OppositeConverter"/>        </Grid.Resources>        <StackPanel Margin="10,10,10,10">            <RadioButton x:Name="myRadio" IsChecked="True">Test convert</RadioButton>            <RadioButton Height="20" Name="redYellow">Yellow</RadioButton>            <RadioButton Height="20" Name="redGreen" VerticalAlignment="Bottom">Green</RadioButton>            <TextBox Text="Sample Text" IsEnabled="{Binding ElementName=myRadio, Path=IsChecked, Converter={StaticResource OppositeConverter}}"/>        </StackPanel>    </Grid>

For example, when test convert is selected, inenable = false of textbox sample text

 
 
 
 
 
 
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.