Microsoft WPF转换器

Max*_*imc 2 c# wpf converter mvvm

所以我今天在MSDN上找到了一个转换器列表,现在我想使用它们中的一些.然而,在搜索了一下后,我似乎无法找到关于它们的任何东西.

我主要想使用IntToBoolConverter.但是我不知道如何使用转换,因为没有提供如何做到(或在谷歌)的信息.

我知道自己很容易制作这个转换器,但我是一名程序员,当你可以制造已经存在的方法(转换器)时,我的机器人很懒.

希望有人能向我解释如何使用这些转换器.

编辑:

在尝试回复后,我在加载usercontrol时遇到错误:

{"Cannot find resource named 'IntToVisibleConverter'. Resource names are case sensitive."}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

并在用户控制上

<TextBlock Text="{Binding TimeLeft}" HorizontalAlignment="Center" Visibility="{Binding Path=TimeLeft, Converter={StaticResource IntToVisibleConverter}}" />
Run Code Online (Sandbox Code Playgroud)

EDIT2:

在usercontrol的Resources中设置它使它工作.太糟糕我不能使用app.xaml由于某种原因我以后会把它弄清楚.谢谢你的帮助,解决了!

格言

sa_*_*213 6

您必须Microsoft.TeamFoundation.Controls.dll在应用程序和xaml中添加作为参考,然后您可以在窗口资源中声明转换器并在您的应用程序中使用.

例:

<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>
Run Code Online (Sandbox Code Playgroud)

如果要在整个应用程序中全局使用转换器(其他窗口/对话框等),可以在中定义转换器 App.xaml

例:

<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>
Run Code Online (Sandbox Code Playgroud)

您可以像第一个示例一样访问它 Converter={StaticResource IntToBoolConverter}