【问题标题】:Microsoft WPF converters微软 WPF 转换器
【发布时间】:2013-01-12 11:47:52
【问题描述】:

所以我今天在 MSDN 上找到了 list 的 Converters,现在我想使用其中的一些。但是经过一番搜索后,我似乎找不到任何关于它们的信息。

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

我知道自己制作这个转换器很容易,但我是一名程序员,如果可以的话,我的 moto 很懒惰,制作已经存在的方法(转换器)是多余的工作。

希望有人可以向我解释如何使用这些转换器。

编辑:

尝试回复后,加载用户控件时出错:

{"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>

在用户控件上

<TextBlock Text="{Binding TimeLeft}" HorizontalAlignment="Center" Visibility="{Binding Path=TimeLeft, Converter={StaticResource IntToVisibleConverter}}" />

编辑2:

将它放在用户控件的资源中使其工作。太糟糕了,由于某种原因我不能使用 app.xaml,我稍后会弄清楚。感谢帮助,已解决!

格言

【问题讨论】:

标签: c# wpf mvvm converter


【解决方案1】:

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

例子:

<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>

如果您想在整个应用程序(其他窗口/对话框等)中全局使用转换器,您可以在 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>

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

【讨论】:

  • 尝试注释掉Textblock并编译应用程序,然后再次取消注释,有时VS不会从App.xaml中获取更改,直到重新编译,或者您可以添加如果您仍有问题,请转换为您的 &lt;UserControl.Resources&gt;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-06
  • 2011-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-12
相关资源
最近更新 更多