英文:
I can't figure out why my Label isn't triggering the converter when inside of span, but does when it isn't? What am I missing here?
问题
我有两个相同数据的标签示例。一个可以工作,但不符合我的要求(它根据社区名称而不是社区名称和用户名的不同颜色对所有文本进行着色),另一个可以工作,但不触发转换器(所以文本显示正常,但不通过转换器进行着色)。我认为第二个不触发转换器的标签是我想要的。
工作的标签,但不符合要求
<Label
x:Name="communityAndUserLabel"
Grid.Row="2"
Grid.Column="1"
FontAttributes="None"
VerticalOptions="End">
<Label.Text>
<MultiBinding StringFormat="{}{0} • {1}" Mode="TwoWay">
<Binding Path="CommunityName" />
<Binding Path="Username" />
</MultiBinding>
</Label.Text>
<Label.TextColor>
<MultiBinding Converter="{StaticResource nameColorMulti}" ConverterParameter="community">
<Binding Path="CommunityName" />
<Binding Path="Username" />
</MultiBinding>
</Label.TextColor>
</Label>
不触发转换器的标签,但仍显示文本
<Label
x:Name="communityAndUserLabel"
Grid.Row="2"
Grid.Column="1"
FontAttributes="None"
VerticalOptions="End">
<Label.FormattedText>
<FormattedString>
<Span Text="{Binding CommunityName, Mode=TwoWay}" TextColor="{Binding CommunityName, Converter={StaticResource nameColorMulti}, ConverterParameter=community}" />
<Span Text=" • " TextColor="Gray" />
<Span Text="{Binding Username, Mode=TwoWay}" TextColor="{Binding Username, Converter={StaticResource nameColorMulti}, ConverterParameter=username}" />
</FormattedString>
</Label.FormattedText>
</Label>
你有没有想到为什么第二个标签不触发转换器的原因?
英文:
I have two Label examples for the same data. One works - but doesn't work like I want it to (it colorized all of the text based on the community name rather than both the community name and the username being different colors), the other works - but doesn't trigger the converter to fire (so the text displays as expected, but doesn't run through the converter for colorization). I think the second one that doesn't trigger the convertor is what I'm after.
Working Label, but doesn't do what I want
<Label
x:Name="communityAndUserLabel"
Grid.Row="2"
Grid.Column="1"
FontAttributes="None"
VerticalOptions="End">
<Label.Text>
<MultiBinding StringFormat="{}{0} • {1}" Mode="TwoWay">
<Binding Path="CommunityName" />
<Binding Path="Username" />
</MultiBinding>
</Label.Text>
<Label.TextColor>
<MultiBinding Converter="{StaticResource nameColorMulti}" ConverterParameter="community">
<Binding Path="CommunityName" />
<Binding Path="Username" />
</MultiBinding>
</Label.TextColor>
</Label>
Label that won't trigger the converter, but still shows the text
<Label
x:Name="communityAndUserLabel"
Grid.Row="2"
Grid.Column="1"
FontAttributes="None"
VerticalOptions="End">
<Label.FormattedText>
<FormattedString>
<Span Text="{Binding CommunityName, Mode=TwoWay}" TextColor="{Binding CommunityName, Converter={StaticResource nameColorMulti}, ConverterParameter=community}" />
<Span Text=" • " TextColor="Gray" />
<Span Text="{Binding Username, Mode=TwoWay}" TextColor="{Binding Username, Converter={StaticResource nameColorMulti}, ConverterParameter=username}" />
</FormattedString>
</Label.FormattedText>
</Label>
Any idea on what I'm overlooking here on why the second one doesn't trigger the converter?
答案1
得分: 1
今天早上我突然意识到,由于我在xaml中将文本分开,我不再需要使用IMultiValueConverter了,所以我创建了两个单独的IValueConverter来完成这个任务:
<Label
x:Name="communityAndUserLabel"
Grid.Row="2"
Grid.Column="1"
FontAttributes="None"
VerticalOptions="End">
<Label.FormattedText>
<FormattedString>
<!-- 使用带有Converter属性的绑定标记扩展 -->
<Span Text="{Binding CommunityName}" TextColor="{Binding CommunityName, Converter={StaticResource communityNameColor}}" />
<Span Text=" • " TextColor="Grey" />
<Span Text="{Binding Username}" TextColor="{Binding Username, Converter={StaticResource usernameColor}}" />
</FormattedString>
</Label.FormattedText>
</Label>
英文:
It dawned on me this morning that since I had separated the text out in the xaml I no longer needed to use a IMultiValueConverter - so instead I created two separate IValueConverters to get this done:
<Label
x:Name="communityAndUserLabel"
Grid.Row="2"
Grid.Column="1"
FontAttributes="None"
VerticalOptions="End">
<Label.FormattedText>
<FormattedString>
<!-- Use Binding markup extensions with Converter attributes -->
<Span Text="{Binding CommunityName}" TextColor="{Binding CommunityName, Converter={StaticResource communityNameColor}}" />
<Span Text=" • " TextColor="Grey" />
<Span Text="{Binding Username}" TextColor="{Binding Username, Converter={StaticResource usernameColor}}" />
</FormattedString>
</Label.FormattedText>
</Label>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论