C# 两个组件是否可能有相同的名称?

C# 两个组件是否可能有相同的名称?,c#,wpf,variables,C#,Wpf,Variables,我正在做一个项目,我需要一个与投影定时器。我设法找到了一种使用两个标签进行投影的方法,但我现在的问题是,我需要给两个标签取相同的名称,以便在所有情况下都以完全相同的方式进行操作,而不需要重复代码。这可能吗 <Grid> <Label x:Name="time" MouseDoubleClick="time_MouseDoubleClick" HorizontalAlignment="Center" FontFamily="Old Eng

我正在做一个项目,我需要一个与投影定时器。我设法找到了一种使用两个标签进行投影的方法,但我现在的问题是,我需要给两个标签取相同的名称,以便在所有情况下都以完全相同的方式进行操作,而不需要重复代码。这可能吗

        <Grid>
            <Label  x:Name="time" MouseDoubleClick="time_MouseDoubleClick" HorizontalAlignment="Center" FontFamily="Old English Text MT"  Margin="0,5,0,0" FontSize="22" Content="00:00:00" FontWeight="Bold" Foreground="Black">
                <Label.RenderTransform>
                    <TranslateTransform X="1.5" Y="1"/>
                </Label.RenderTransform>
            </Label>
            <Label x:Name="time2"  MouseDoubleClick="time_MouseDoubleClick" HorizontalAlignment="Center" FontFamily="Old English Text MT"  Margin="0,5,0,0" FontSize="22" Content="00:00:00" FontWeight="Bold" Foreground="White"/>
        </Grid>


这就是代码创建放置阴影的方式。有办法解决这个问题吗?或者是否有一种类似于HTML的方法可以定义“类”名称?

您不需要两个标签。你需要:


您不需要两个标签。你需要:


看起来您实际上需要一个drop shaddow效果,另一个基于相同属性更新两个控件的实现可能需要绑定

在codebehind文件的initialize方法中,设置DataContext

DataContext = this;
更改xaml以绑定到公共字符串属性

  <Grid>
        <Label  x:Name="time" MouseDoubleClick="time_MouseDoubleClick" HorizontalAlignment="Center" FontFamily="Old English Text MT"  Margin="0,5,0,0" FontSize="22" Content="{Binding Time}" FontWeight="Bold" Foreground="Black">
            <Label.RenderTransform>
                <TranslateTransform X="1.5" Y="1"/>
            </Label.RenderTransform>
        </Label>
        <Label x:Name="time2"  MouseDoubleClick="time_MouseDoubleClick" HorizontalAlignment="Center" FontFamily="Old English Text MT"  Margin="0,5,0,0" FontSize="22" Content="{Binding Time}" FontWeight="Bold" Foreground="White"/>
    </Grid>
代码隐藏(.cs文件)应实现INotifyPropertyChanged接口,并且属性应引发事件


然后,通过更改Time属性,XAML将自动更新。

看起来您实际上需要drop shaddow效果,另一个基于相同属性更新两个控件的实现可能在于绑定

在codebehind文件的initialize方法中,设置DataContext

DataContext = this;
更改xaml以绑定到公共字符串属性

  <Grid>
        <Label  x:Name="time" MouseDoubleClick="time_MouseDoubleClick" HorizontalAlignment="Center" FontFamily="Old English Text MT"  Margin="0,5,0,0" FontSize="22" Content="{Binding Time}" FontWeight="Bold" Foreground="Black">
            <Label.RenderTransform>
                <TranslateTransform X="1.5" Y="1"/>
            </Label.RenderTransform>
        </Label>
        <Label x:Name="time2"  MouseDoubleClick="time_MouseDoubleClick" HorizontalAlignment="Center" FontFamily="Old English Text MT"  Margin="0,5,0,0" FontSize="22" Content="{Binding Time}" FontWeight="Bold" Foreground="White"/>
    </Grid>
代码隐藏(.cs文件)应实现INotifyPropertyChanged接口,并且属性应引发事件


然后通过更改时间属性,XAML将自动更新。

让我们重新发明所有WPF效果吧!:)让我们重新发明所有WPF效果吧!:)哦,实际上还有一个!谢谢你,伙计+1OH,实际上添加了一个!谢谢你,伙计+1.