3

我想处理Button.Click实现UserControl.

UserControl看起来像这样:

<UserControl>
    <Grid x:Name="LayoutRoot" Background="Transparent">

        <Button Command="{Binding ButtonClickedCommand, Source={RelativeSource Self}}" />

    </Grid>
</UserControl>

以及背后的代码:

public partial class MapUserControl : UserControl
{

    public MapTabBar()
    {
        InitializeComponent();
    }

    public ICommand ButtonClickedCommand
    {
        get { return (ICommand)GetValue(ButtonClickedCommandProperty); }
        set { SetValue(ButtonClickedCommandProperty, value); }
    }

    public static readonly DependencyProperty ButtonClickedCommandProperty = DependencyProperty.Register(
        "ButtonClickedCommandProperty", 
        typeof(ICommand), 
        typeof(MapTabBar), 
        new PropertyMetadata(null));

}

UserControl在我的MainPage.xaml:

<Grid x:Name="LayoutRoot" Background="Transparent">

    <maps:Map x:Name="Map" ZoomLevel="7" />

    <local:MapUserControl
        ButtonClickedCommand="{Binding ???}" />

</Grid>

如何将此绑定ButtonClickedCommand到我的函数MainPage.xaml.cs

    protected void Button_Clicked()
    {
        MessageBox.Show("button clicked");
    }

最终,我想在我的 MainPage 中操作地图(居中、过滤、重新加载)。这是正确的方法 - 还是有其他方法?

4

3 回答 3

1

如果你想要的只是注册你的主页的一个功能并且没有能力从你的viewModel绑定一个命令,我看不出你使用命令有什么用,只需使用事件:

在您的 UserControl 中定义:

public event RoutedEventHandler ButtonClicked;

    private void MyButtonClicked(object sender, RoutedEventArgs e)
    {
        if (ButtonClicked != null)
        {
            ButtonClicked(sender, e);
        }

    }

然后在你的主页上像这样使用它:

    <local:MapUserControl ButtonClicked="Button_Clicked"/>
于 2013-09-20T21:36:11.720 回答
1

在您的 UserControl 中尝试以下操作

<!-- xaml -->
<Button Click="OnButtonClicked"/>


// Code behind
private void OnButtonClicked(object sender, RoutedEventArgs routedEventArgs)
{
    if(ButtonClickedCommand != null)
    {
        ButtonClickedCommand.Execute(null);
    }
}

另一种解决方案是让您的地图工具控制地图本身。您需要在地图工具对象上设置 DP(旁注:最好创建自定义控件而不是 USerControl)。

// MapTools
public Map Map
{
    get { return (Map)GetValue(MapProperty); }
    set { SetValue(MapProperty, value); }
}

public static readonly DependencyProperty MapProperty =
    DependencyProperty.Register(
    "Map", 
    typeof(Map), 
    typeof(MapTools), 
    new PropertyMetadata(null));

private void OnButtonClicked(object sender, RoutedEventArgs routedEventArgs)
{
    // Manipulate map
}

然后在您使用它的页面中

<Grid>
    <maps:Map x:Name="MyMap"\>
    <mytools:MapTools Map="{Binding ElementName=MyMap}"/>
</Grid>
于 2013-09-20T20:15:32.097 回答
0

MainPage.xaml, I am binding a login UserControl by using the namespace : xmlns:UC="clr-namespace:Test.Views" , since I have my usercontrol in Folder named "Views".

<ScrollViewer>
<UC:Login BtnLoginClick="Login_BtnLoginClick"/>
</ScrollViewer>

Login.cs

public partial class Login : UserControl    {

    public event EventHandler BtnLoginClick;

    public Login()
    {
        InitializeComponent();
    }

    private void btnLogin_Click(object sender, RoutedEventArgs e)
    {
        string userName = txtUserName.Text;
        string userPassword = txtUserPassword.Password.Trim();
        if (userName != null && userName != string.Empty && userPassword != null &&       userPassword != string.Empty)
        {
            if (this.BtnLoginClick != null)
            {
                this.BtnLoginClick(this, e);
            }
        }
        else
        {
            MessageBox.Show("Invalid username or password");
        }
    }

}

Finally, dont forgot to use the event handler in MainPage.xaml to capture the button clicked event from Login Usercontrol to do other actions.

MainPage.xaml

<UC:Login BtnLoginClick="Login_BtnLoginClick"/>

Here "BtnLoginClick" its an event Handler defined in the Login.xaml[User Control].

Create new event for this "BtnLoginClick" event as i created "Login_BtnLoginClick".

MainPage.cs

private void Login_BtnLoginClick(object sender, EventArgs e)
{
Messagebox.Show("Event captured successfully");
////Here you can add your stuffs...     
}
于 2013-11-06T07:07:30.060 回答