【问题标题】:Chaging Border Background Color on Texblock Value?更改文本块值的边框背景颜色?
【发布时间】:2011-02-25 20:43:06
【问题描述】:

如果子 texblock 的值为“0”,我正在尝试将边框背景颜色更改为红色。我正在尝试使用 GreaterThanZero 转换器,但它什么也没做。我想知道是否有人可以提供帮助。如果值大于零,我还需要将 texblock 前景色更改为白色。任何想法都受到高度赞赏。提前谢谢你。

这是我的 XAML:

<Grid>
<Border x:Name="TextBoxBorder" BorderThickness="1" Width="30">
   <TextBlock x:Name="TotalTileValue" Text="{Binding TotalItemCount}">
    <TextBlock.Style>      
        <Style>        
            <Style.Triggers>          
                <DataTrigger Binding="{Binding Content, Converter={x:Static local:GreaterThanZeroConverter.Instance}, RelativeSource={RelativeSource Self}}" Value="True" >            
                      <Setter Property="TextBlock.Foreground" Value="White" />  
                      <Setter TargetName="TextBoxBorder" Property="Background" Value="#FFCC0000" />     
                </DataTrigger>        
            </Style.Triggers>      
        </Style>    
    </TextBlock.Style>             
   </TextBlock>
</Border>

【问题讨论】:

    标签: wpf wpf-controls binding


    【解决方案1】:

    是否要处理 TextBox 的文本?尝试绑定到 Text-Property 而不是 content-Property。

    <DataTrigger Binding="{Binding Text, Converter={x:Static local:GreaterThanZeroConverter.Instance}, RelativeSource={RelativeSource Self}}" Value="True" >             
    

    然而,也许这只是故事的一半。这取决于转换器。如果它处理字符串值,它会这样做。

    编辑
    根据需要,这里开始介绍如何构建您自己的价值转换器。请注意,我不知道您的确切要求。我也没有测试过代码。它应该只是您自己的转换器的起点:

    public class GreaterThanZeroValueConverter : IValueConverter{
    
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
            if (null == value) {
                return false;
            }
            int intValue = 0;
            if (value is string) {                
                if (!Int32.TryParse((string)value,out intValue)) {
                    return false;
                }                
            }
            if (value is int) {
                intValue = (int)value;
            } else {
                throw new InvalidOperationException("Unsupported Type ["+value.GetType().Name+"]");
            }
            return intValue > 0;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
            throw new NotImplementedException();
        }
    }
    

    【讨论】:

    • 很遗憾,我做不到。这样指定的“文本”转换无效。感谢您提供帮助。
    • @vladc77:这就是我最后一行的意思。绑定到内容是没有用的,因为您在 TextBlock 上没有 Content-Property。因此什么也没有发生。但是,如果您绑定到 Text,则绑定有效,但由于您的转换器不知道如何将字符串转换为 int,因此会引发异常。您必须为字符串值扩展转换器。类似于: if(value is string){int intValue=0;Int32.TryParse((string)value,out intValue);return intValue>0;}...
    • 我无权访问转换器。它被打包在 dll 中。看来我需要创建一个新的。我想知道你是否有任何好的样品。再次感谢您。
    • @vladc77:我已经更新了我的答案。我没有测试过它,但我认为它会做。我希望这值得点赞:)
    • 非常感谢!我会努力的。它看起来像我需要的。我仍然无法使用此转换器将转换器应用于 Text 属性。我希望我能找到问题所在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    • 2018-12-25
    • 1970-01-01
    • 2011-01-29
    相关资源
    最近更新 更多