【问题标题】:how to apply a style using a converter in wpf c#?如何在 wpf c# 中使用转换器应用样式?
【发布时间】:2013-12-05 15:07:53
【问题描述】:

我正在开发一个带有语音命令的系统,该系统适用于参数网格。 我想对正在编辑的元素应用一种样式,以便用户知道他在哪里...

MyView.xaml

 <telerik:RadNumericUpDown Name={Binding Element[0].ID}  Grid.Column="0" Name="Left" MinWidth="15" FontSize="11" Minimum="0" NumberDecimalDigits="0" 
BorderThickness="0" Maximum="30" 
IsInteger="True" ShowButtons="False" ShowTextBox="True" 
HorizontalContentAlignment="Left"  Value="{Binding Element[0].Input, Mode=TwoWay, ElementName=InputViewUserControl}" Background="Transparent" Foreground="#FF858585"  />


 <telerik:RadNumericUpDown Name={Binding Element[1].ID}  Grid.Column="0" Name="Left" MinWidth="15" FontSize="11" Minimum="0" NumberDecimalDigits="0" 
BorderThickness="0" Maximum="30" 
IsInteger="True" ShowButtons="False" ShowTextBox="True" 
HorizontalContentAlignment="Left"  Value="{Binding Element[1].Input, Mode=TwoWay, ElementName=InputViewUserControl}" Background="Transparent" Foreground="#FF858585"  />

 <telerik:RadNumericUpDown Name={Binding Element[2].ID}  Grid.Column="0" Name="Left" MinWidth="15" FontSize="11" Minimum="0" NumberDecimalDigits="0" 
BorderThickness="0" Maximum="30" 
IsInteger="True" ShowButtons="False" ShowTextBox="True" 
HorizontalContentAlignment="Left"  Value="{Binding Element[2].Input, Mode=TwoWay, ElementName=InputViewUserControl}" Background="Transparent" Foreground="#FF858585"  />

.....我有 30 个元素所以...

如果用户说:元素一,我想将样式应用于元素[0]

如果你有什么想法,请告诉我谢谢:)

【问题讨论】:

  • 考虑改变 LostFocus、GotFocus 的风格并根据您的语音处理功能设置焦点。
  • ....i have 30 elements So... - 请立即删除所有可怕的重复并使用ItemsControl
  • @Allel 问题是如何知道声音是什么,或者知道声音是什么之后,如何改变风格?
  • :) 这是我的问题的解决方案stackoverflow.com/questions/1356045/…

标签: c# wpf xaml telerik


【解决方案1】:

您只需要在Resources 部分中添加一个Style,然后您需要将一个bool IsSelected 属性添加到您的Element 类中:

public bool IsSelected { get; set; } // Implement INotifyPropertyChanged interface here

<Style TargetType="{x:Type telerik:RadNumericUpDown}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsSelected}" Value="True">
            <Setter Property="Background" Value="LightGreen" />
        </DataTrigger>
    </Style.Triggers>
</Style>

Style 将为具有设置为TrueIsSelected 属性的对象的Background 着色。您现在需要做的就是将当前对象的IsSelected 属性设置为True,并将前一个对象的IsSelected 值设置为False

请注意,此 Style 没有 x:Key 值...这意味着它将在您的所有控件上隐式设置,而无需手动在每个元素上设置 Style

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-06
    • 2022-01-17
    相关资源
    最近更新 更多