c# - DataContext 没有在带有路径数据的自定义按钮中设置

标签 c# xaml mvvm data-binding windows-phone

您好,我正在尝试制作一个带有 PathData 的自定义按钮。到目前为止,我已经设法查看了其中的路径。但是我的 Button 没有接受 MVVM 命令。

自定义按钮 XAML

<Button x:Class="My_Class.CustomControls.MyButtonWithPath"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="480" d:DesignWidth="480">
    <Grid>
        <Path  Data="{Binding}" Name="path"/>
    </Grid>
</Button>

后面的按钮代码
public partial class MyButtonWithPath : Button
{
    public MyButtonWithPath()
    {
        InitializeComponent();
        this.DataContext = this;
    }

    private static string _pathDatay;
    public string PathDatay
    {
        get { return _pathDatay; }
        set { _pathDatay = value; }
    }

    public static readonly DependencyProperty PathDataProperty =
        DependencyProperty.Register("PathDatay", typeof(string), typeof(MyButtonWithPath), new PropertyMetadata(pathDataCallback));

    private static void pathDataCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        _pathDatay = (string)e.NewValue;
    }
}

XAML 中的用法
<converters:KeyToValueConverter x:Key="conv"/>
<custom:MyButtonWithPath PathDatay="{Binding ConverterParameter=tv, Converter={StaticResource conv}}" />

转换器
 public class KeyToValueConverter : IValueConverter
 {
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        try
        {
            return Utils.GetPathData(parameter.ToString());
        }
        catch (Exception c)
        {
            throw c;
        }

    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new System.NotImplementedException();
    }
}

实用类
public static string GetPathData(string key)
    {
        Dictionary<string, string> dictionary = new Dictionary<string, string>();

        dictionary.Add("tv", "etc etc etc......");
        return dictionary[key];
    }

问题

每当我在按钮的用法中编写命令时,它会显示未找到错误,因为它在我的自定义按钮而不是我的 MainViewModel 中查找命令(仅供引用,我有一个名为 MainViewModel 的 ViewModel,我将在其中放置与命令相关的代码)

我的猜测

我正在使用此“this.DataContext=this;”将 Button 的 DataContext 设置为自身但如果我省略这一行,则路径不会显示。请指导我正确设置这些东西

解决方案

下面给出

最佳答案

你是对的,this.DataContext=this;这条线是问题所在。
您通常不使用数据绑定(bind)来设置自定义控件的外观。相反,您应该设置 NamePath并设置其Data初始化时的属性。您可以通过覆盖 OnApplyTemplate 来做到这一点:

public override void OnApplyTemplate()
{
    base.OnApplyTemplate();
    Path p = GetTemplateChild("path") as Path;
    p.Data = ...
}

关于c# - DataContext 没有在带有路径数据的自定义按钮中设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34114067/

相关文章:

c# - 在自定义 InvalidModelStateResponseFactory asp.net core 2.2 中读取请求正文

javascript - knockout 计算属性输出函数体

c# - MVVM:如何处理嵌套 ViewModel 之间的交互?

c# - 如何在 Visual Studio 2015 及更高版本中打开空白应用程序 (XAML)? - C#

android - 如何使用android架构组件实现登录 Activity 架构?

c# - 为什么找不到我的 ViewModel?

c# - FileInfo.OpenRead() - 它使用什么类型的编码?

c# - 以管理员身份调试 Visual Studio 2013

c# - MVVM:来自 FileOpenPicker 的图像绑定(bind)源

c# - 通过 XAML 调用静态类中的静态方法