[英]How to use targetname with usercontrol and trigger
I made UserControl of DataGrid. 我做了DataGrid的UserControl。 I placed this new component into page1.xaml. 我将此新组件放入page1.xaml。 I would like to use some template and setting based on value in Data1. 我想基于Data1中的值使用一些模板和设置。
Could you help me with this code how to avoid the error message? 您能否用这段代码帮助我,如何避免出现错误消息?
<my:MyDataGrid Grid.Column="1" Grid.Row="1" HorizontalAlignment="Left" Margin="29,295,0,0" Name="myDataGrid1"
VerticalAlignment="Top" Height="151" Width="176" SelectionChanged="myDataGrid1_SelectionChanged">
<my:MyDataGrid.Columns>
<DataGridTemplateColumn Header="Col1" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Data1}" x:Name="mytext" />
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Data1}" Value="1">
<Setter TargetName="mytext" Property="Foreground" Value="Red" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</my:MyDataGrid.Columns>
</my:MyDataGrid>
I got error message: 我收到错误消息:
Cannot set Name attribute value 'mytext' on element 'TextBlock'. 无法在元素“ TextBlock”上设置“名称”属性值“ mytext”。 'TextBlock' is under the scope of element 'MyDataGrid', which already had a name registered when it was defined in another scope. “ TextBlock”在元素“ MyDataGrid”的范围内,该元素在另一个范围内定义时已注册了名称。
You can add you data trigger to a style attached to a TextBlock 您可以将数据触发器添加到附加到TextBlock的样式中
<my:MyDataGrid Grid.Column="1" Grid.Row="1" ...>
<DataGrid.Columns>
<DataGridTemplateColumn Header="Col1" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Data1}" x:Name="mytext">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding Data1}" Value="1">
<Setter Property="Foreground" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</my:MyDataGrid>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.