[英]WPF Converter makes control invisible
我在WPF控件上遇到問題,正在像這樣實現
<UserControl x:Class="VizarisUpdater.Page.SetupPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:VizarisUpdater.Converters"
mc:Ignorable="d"
d:DesignHeight="390" d:DesignWidth="692">
頁面稍后
<Label Grid.Row="2" Grid.Column="1">
<TextBlock Text = "{Binding Path=SpaceRequired, StringFormat='Space Required: {0}', Converter={local:NumberToBestSizeConverter}}" FontSize="20" VerticalAlignment="Center"/>
</Label>
當我卸下轉換器時,它顯示數字為ok(不進行轉換),但是當我添加轉換器時,它消失了。 我也有智慧告訴我:
The name "NumberToBestSizeConverter" does not exists in the namespace "clr-namespace:VizarisUpdater.Converters"
我在項目中定義了.net Framework 4.0。 有任何想法嗎?
這可能是因為您必須在XAML
聲明轉換器
<UserControl.Resources>
<local:NumberToBestSizeConverter x:Key="NumberToBestSizeConverter" />
</UserControl.Resources>
然后,您可以在代碼中使用Key
:
<Label Grid.Row="2" Grid.Column="1">
<TextBlock Text = "{Binding Path=SpaceRequired, StringFormat='Space Required: {0}', Converter={StaticResource NumberToBestSizeConverter}}" FontSize="20" VerticalAlignment="Center"/>
</Label>
請注意,在Converter
屬性中使用StaticResource
。
創建轉換器的實例
<Window.Resources>
<NumberToBestSizeConverter x:Key="converterName"/>
</Window.Resources>
比你的束縛
Converter={StaticResource converterName}}"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.