gpt4 book ai didi

c# - 微软 WPF 转换器

转载 作者:行者123 更新时间:2023-12-02 07:42:14 26 4
gpt4 key购买 nike

所以我找到了list今天 MSDN 上有很多转换器,现在我想使用其中的一些。然而,经过一番搜索后,我似乎找不到任何关于它们的信息。

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

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

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

编辑:

尝试回复后,我在加载用户控件时遇到错误:

{"Cannot find resource named 'IntToVisibleConverter'. Resource names are case sensitive."}

应用程序.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,稍后我会解决它。感谢您的帮助,已解决!

马克西姆

最佳答案

您必须添加 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>

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

关于c# - 微软 WPF 转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14573895/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com