如何在wpf c#中使用转换器应用样式?

时间:2013-12-05 15:07:53

标签: c# wpf xaml telerik

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

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个元素所以......

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

如果您有任何想法让我知道谢谢:)

1 个答案:

答案 0 :(得分:2)

您只需要Style部分中的一个Resources,然后您需要向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会为具有Background属性设置为IsSelected的对象的True着色。您现在需要做的就是将当前对象的IsSelected属性设置为True,并将上一个对象的IsSelected值设置为False

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