我有这个翻转视图:
<FlipView x:Name="models_list" SelectionChanged="selectionChanged">
<FlipView.ItemTemplate>
<DataTemplate>
<Grid x:Name="cv">
<Image x:Name="img1" Source = "{Binding ModelImage}" Stretch="Fill" Tag="{Binding ModelTag}"/>
</Grid>
</DataTemplate>
</FlipView.ItemTemplate>
Run Code Online (Sandbox Code Playgroud)
我想找到当前所选索引的img1.在搜索它时,我在这里的一些帖子中找到了这个方法:
private DependencyObject FindChildControl<T>(DependencyObject control, string ctrlName)
{
int childNumber = VisualTreeHelper.GetChildrenCount(control);
for (int i = 0; i < childNumber; i++)
{
DependencyObject child = VisualTreeHelper.GetChild(control, i);
FrameworkElement fe = child as FrameworkElement;
// Not a framework element or is null
if (fe == null) return null;
if (child is T && fe.Name== ctrlName)
{
// Found the control so …Run Code Online (Sandbox Code Playgroud) 我在Windows 2012中有一个WPF项目,我需要在Window Loaded事件中加载一些信息.不过,我需要在View Model中而不是在CodeBehind中执行此操作.我试图使用以下代码:
在我的xaml中:
<interactivity:Interaction.Behaviors>
<behaviors:WindowLoadedBehavior LoadedCommand="{Binding WindowLoadedCommand}" />
</interactivity:Interaction.Behaviors>
Run Code Online (Sandbox Code Playgroud)
在我的视图模型中:
private DelegateCommand _WindowLoadedCommand;
public DelegateCommand WindowLoadedCommand
{
get
{
return _WindowLoadedCommand;
}
private set
{
_WindowLoadedCommand = value;
}
}
public ShellViewModel()
{
WindowLoadedCommand = new DelegateCommand(WindowLoadedAction);
}
protected void WindowLoadedAction()
{
...
}
Run Code Online (Sandbox Code Playgroud)
我附加的行为:
public class WindowLoadedBehavior : Behavior<FrameworkElement>
{
[SuppressMessage("Microsoft.StyleCop.CSharp.MaintainabilityRules", "SA1401:FieldsMustBePrivate", Justification = "Dependency Property. Allow public.")]
public static DependencyProperty LoadedCommandProperty = DependencyProperty.Register("LoadedCommand", typeof(ICommand), typeof(WindowLoadedBehavior), new PropertyMetadata(null));
public ICommand LoadedCommand
{
get { return (ICommand)GetValue(LoadedCommandProperty); } …Run Code Online (Sandbox Code Playgroud)