简体   繁体   English

如何回调自定义控件 WPF 中的代码

[英]How to callback to code behind in custom control WPF

I am using wpf.我正在使用 wpf。 I created a custom control.我创建了一个自定义控件。 In this control, I have multiple buttons.在这个控件中,我有多个按钮。 I can bind the button commands to the viewmodel.我可以将按钮命令绑定到视图模型。 In this application, however, I need to callback to the code back file and not the viewmdoel.然而,在这个应用程序中,我需要回调到代码返回文件而不是 viewmdoel。 How can I do that?我怎样才能做到这一点?

Here is the example of my custom control:这是我的自定义控件的示例:

<Grid>
    <StackPanel Orientation="Horizontal" Grid.Row="3">
        <Button Content="Button A" Command="{Binding SwitchToABCCommand}">
            <Button.Style>
                <Style TargetType="Button">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding CurRegion}" Value="ABC">
                            <Setter Property="Foreground" Value="Red" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Button.Style>
        </Button>
        <Button Content="Button B" Command="{Binding SwitchToEFGCommand}">
            <Button.Style>
                <Style TargetType="Button">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding CurRegion}" Value="DEF">
                            <Setter Property="Foreground" Value="Red" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Button.Style>
        </Button>
    </StackPanel>
</Grid>

Then I have another window using this custom control.然后我有另一个 window 使用这个自定义控件。

MainWindowViewModel.cs MainWindowViewModel.cs

public ICommand SwitchToABCCommand { get; }
public ICommand SwitchToDEFCommand { get; }
public MainWindowViewModel()
{
    SwitchToABCCommand = new DelegateCommand(HandleSwitchToABCCommand);
    SwitchToEFGCommand = new DelegateCommand(HandleSwitchToEFGCommand);
}
private void HandleSwitchToABCCommand()
{

}
private void HandleSwitchToDEFCommand()
{

}

And I want to callback to MainWindow.xaml.cs我想回调到 MainWindow.xaml.cs

private void SwitchToABC(object sender, RoutedEventArgs e)
{
}
private void SwitchToDEF(object sender, RoutedEventArgs e)
{
}

I have tried to do:我试图这样做:

<Button Content="Button A" Click="SwitchToABC">

But this only can callback to MyCustomControl.xaml.cs .但这只能回调到MyCustomControl.xaml.cs How can I call back to MainWindow.xaml.cs?如何回调 MainWindow.xaml.cs?

Thank you谢谢

The event handler must be defined in the same class as the XAML markup.事件处理程序必须在与 XAML 标记相同的 class 中定义。 If you define SwitchToABC in MyCustomControl.xaml.cs , you can call an event handler from the MainWindow from there though:如果您在MyCustomControl.xaml.cs中定义SwitchToABC ,您可以从那里调用MainWindow的事件处理程序:

private void SwitchToABC(object sender, RoutedEventArgs e)
{
    MainWindow parentWindow = Window.GetWindow(this) as MainWindow;
    if (parentWindow != null)
    {
        parentWindow.SomeHandler(sender, e);
    }
}

Note that this create a strong coupling between the control and the window.请注意,这会在控件和 window 之间产生强耦合。

Binding to a SwitchToABCCommand command in the control XAML also creates an indirect coupling to the view model.绑定到控件 XAML 中的SwitchToABCCommand命令还会创建与视图 model 的间接耦合。 A better way would be to add a command property to the control and bind this one to the view model command property, eg:更好的方法是将命令属性添加到控件并将其绑定到视图 model 命令属性,例如:

<MyCustomControl YourCommand="{Binding SwitchToABCCommand }"  />

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

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