相关疑难解决方法(0)

如何访问XAML DataTemplate中的控件?

我有这个翻转视图:

<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)

c# windows-8 winrt-xaml windows-store-apps flipview

26
推荐指数
2
解决办法
5万
查看次数

窗口加载和WPF

我在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)

c# wpf events xaml mvvm

7
推荐指数
1
解决办法
5万
查看次数

标签 统计

c# ×2

events ×1

flipview ×1

mvvm ×1

windows-8 ×1

windows-store-apps ×1

winrt-xaml ×1

wpf ×1

xaml ×1