问题
So I found a list of Converters on MSDN today, now I want to use some of them. However after searching a bit I can t seem to find ANYTHING about them.
I mainly want to use the IntToBoolConverter. However I got no clue how to use the convert since there is no info provided how to do it(or on google).
I know it is easy to make this converter myself, but I am a programmer and my moto is be lazy when you can and making methods(converters) that already exists is redundant work.
Hope someone can explain to me how to use these converters.
EDIT:
After trying the reply I get error on loading the usercontrol:
{"Cannot find resource named 'IntToVisibleConverter'. Resource names are case sensitive."}
App.xaml
<Application x:Class="Smartp1ck.JungleTimerClient.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:msconv="clr-namespace:Microsoft.TeamFoundation.Controls.WPF.Converters;assembly=Microsoft.TeamFoundation.Controls">
<Application.Resources>
<msconv:IntToVisibleConverter x:Key="IntToVisibleConverter" />
</Application.Resources>
</Application>
And on the usercontrol
<TextBlock Text="{Binding TimeLeft}" HorizontalAlignment="Center" Visibility="{Binding Path=TimeLeft, Converter={StaticResource IntToVisibleConverter}}" />
EDIT2:
Puting it in the Resources of the usercontrol makes it work. Too bad I can t use the app.xaml for some reason I ll figuere it out later. Thanks for the help, solved!
Maxim
回答1:
You will have to add Microsoft.TeamFoundation.Controls.dll
as a reference in your app and xaml, then you can declare the converter in your window resources and use in your application.
Example:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mstf="clr-namespace:Microsoft.TeamFoundation.Controls.WPF.Converters;assembly=Microsoft.TeamFoundation.Controls"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<mstf:IntToBoolConverter x:Key="IntToBoolConverter" />
</Window.Resources>
<Grid>
<CheckBox IsChecked="{Binding Path=MyInt, Converter={StaticResource IntToBoolConverter}}" />
</Grid>
</Window>
If you want to use the converter globally throughout your application (other windows/dialogs etc) you can define the converter in the App.xaml
Example:
<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mstf="clr-namespace:Microsoft.TeamFoundation.Controls.WPF.Converters;assembly=Microsoft.TeamFoundation.Controls"
StartupUri="MainWindow.xaml">
<Application.Resources>
<mstf:IntToBoolConverter x:Key="IntToBoolConverter" />
</Application.Resources>
</Application>
you can access this the same as the first example Converter={StaticResource IntToBoolConverter}
来源:https://stackoverflow.com/questions/14573895/microsoft-wpf-converters