ViewModel和ControlTemplate中的访问形状

时间:2016-06-24 12:37:23

标签: c# wpf

我有一个按钮。我需要访问Template.But中的形状我无法让它为我工作。 我的模板:

<Grid x:Name="root">
 <Ellipse x:Name="border" Width="50" Height="50" Fill="Azure"/>
 <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>

视图模型

public ICommand CmdAction {
 get {
  cmdAction = cmdAction ?? new RelayCommand(x => Action(), x => true);
  return cmAction;
 }
}

void Action()
{
     Storyboard board = Application.Current.FindResource("Storyborder") as Storyboard;
     var shapeborder = Application.Current.FindResource("border") as Ellipse;
     if (!mode) {
      shapeborder.Visibility = Visibility.Visible;
      board.Begin(shapeborder, true);
      mode = true;
     } else {
      mode = false;
      board.Stop(shapeborder);
      shapeborder.Visibility = Visibility.Hidden;
     }
}

我试过了:

var btn=((Window1)Application.Current.MainWindow).bt;
Grid grid = btn.Template.FindName("root",btn) as Grid;
var lb = grid.FindName("border") as Ellipse;

0 个答案:

没有答案