我正在开发一个具有适用于参数网格的语音命令的系统。 我想将一个样式应用于正在编辑的元素,以便用户知道他在哪里声音......
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]
如果您有任何想法让我知道谢谢:)
答案 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