简体   繁体   English

WPF MVVM Binding x:Name 不是触发方法

[英]WPF MVVM Binding x:Name not triggering method

I'm new to WPF, started trying to learn it on Wednesday.我是 WPF 的新手,周三开始尝试学习。 I have started looking into Caliburn.Micro and MVVM and I have used MaterialDesignInXAML Nuget package for a material design in the app.我已经开始研究 Caliburn.Micro 和 MVVM,并在应用程序中使用 MaterialDesignInXAML Nuget package 进行材料设计。 From this I have created a basic application that I want to use.由此我创建了一个我想使用的基本应用程序。 This app has drawer menu that slides out from the side.这个应用程序有从侧面滑出的抽屉菜单。 On this menu, I have a number of buttons.在这个菜单上,我有许多按钮。 I want to be able to click these buttons and have my ContentControl Update with the relevant UserControl Views I have created.我希望能够单击这些按钮并使用我创建的相关 UserControl 视图更新我的 ContentControl。 I have LoadHome() in the public ShellViewModel(){LoadHome();} Method and this loads when i start the app, however clicking the buttons in the menu does not update the contentcontrol nor does it trigger any break points, even thought it is using the same x:Name="" syntax the others use.我在公共ShellViewModel(){LoadHome();}方法中有LoadHome() ,当我启动应用程序时它会加载,但是单击菜单中的按钮不会更新 contentcontrol 也不会触发任何断点,即使是这样使用与其他人相同的x:Name=""语法。 If i take the buttons off the menu and place them after, The button click works as expected and updates the contentcontrol.如果我将按钮从菜单中取出并将它们放在之后,按钮单击按预期工作并更新内容控件。

My Code我的代码

ShellView.XAML ShellView.XAML

 <DockPanel>
                           <Button x:Name="LoadHome"
                            Margin="4" HorizontalAlignment="Center"
                            Style="{DynamicResource MaterialDesignFlatButton}">
                        HOME
                    </Button>
                    <Button x:Name="LoadBlobPurge"
                            Margin="4" HorizontalAlignment="Center"
                            Style="{DynamicResource MaterialDesignFlatButton}"
                            materialDesign:RippleAssist.Feedback="{Binding RelativeSource={RelativeSource 
    Self}, Path=Foreground, Converter={StaticResource BrushRoundConverter}}"
    >
                        BLOB PURGE TOOL
                    </Button>
                    <Button Command="{x:Static materialDesign:DrawerHost.CloseDrawerCommand}"
                            Margin="4" HorizontalAlignment="Center"
                            Style="{DynamicResource MaterialDesignFlatButton}">
                        APP 2
                    </Button>
                    <Button Command="{x:Static materialDesign:DrawerHost.CloseDrawerCommand}"
                            Margin="4" HorizontalAlignment="Center"
                            Style="{DynamicResource MaterialDesignFlatButton}">
                        APP 3
                    </Button>
                    <Button Command="{x:Static materialDesign:DrawerHost.CloseDrawerCommand}"
                            Margin="4" HorizontalAlignment="Center"
                            Style="{DynamicResource MaterialDesignFlatButton}">
                        CLOSE
                    </Button>
                    <Button x:Name="LoadHowTo"
                            Margin="4" HorizontalAlignment="Center"
                            Style="{DynamicResource MaterialDesignFlatButton}">
                        <materialDesign:PackIcon Kind="About" />
                    </Button>
                    
**ShellViewModel.cs**

        public class ShellViewModel : Conductor<object>
    {
        public ShellViewModel()
        {
            LoadHome();
        }

        public void LoadHome()
        {
            ActivateItem(new HomeViewModel());
        }

        public void LoadBlobPurge()
        {
            ActivateItem(new BlobPurgeViewModel());
        }

        public void LoadHowTo()
        {
            ActivateItem(new HowToViewModel());
        }

        public void LoadApp2()
        {
            
        }

        public void LoadApp3()
        {
            
        }

    }

As you are using Caliburn.Micro you can try to use the following syntax on the button.当您使用 Caliburn.Micro 时,您可以尝试在按钮上使用以下语法。 x:Name binding also failed me when using MaterialDesignInXAML. x:Name 绑定在使用 MaterialDesignInXAML 时也失败了。

<Button cal:Message.Attach="[Event Click] = [Action LoadBlobPurge()]"
                    Margin="4" HorizontalAlignment="Center"
                        Style="{DynamicResource MaterialDesignFlatButton}"
                        materialDesign:RippleAssist.Feedback="{Binding RelativeSource= 
   {RelativeSource Self}, Path=Foreground, Converter={StaticResource BrushRoundConverter}}">                       
LOAD BLOB PURGE </Button>

I found a more in depth answer in relation to this here:我在这里找到了与此相关的更深入的答案:

Caliburn Micro void function can't be triggered inside DialogHost Caliburn Micro void function 无法在 DialogHost 内部触发

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM