简体   繁体   English

具有自定义属性的WPF Usercontrol

[英]WPF Usercontrol with custom properties

I want to outsource two images to a custom usercontrol, which should have two properties to be set, one for the source of each image. 我想将两个图像外包给自定义用户控件,该用户控件应该设置两个属性,一个用于每个图像的源。

But I ran into trouble with the datacontext, which isnt recognised correctly. 但是我遇到了无法正确识别的datacontext。 It might also be a problem, that its the first time that I use dependency properties. 它也可能是一个问题,它是我第一次使用依赖属性。 Anyway, I hope you can figure out my thoughts and help me here, here comes the sourcecode: 无论如何,我希望你能弄清楚我的想法并在这里帮助我,这里有源代码:

MainViewModel: MainViewModel:

public class MainWindowViewModel : INotifyPropertyChanged
{
    private string _spielerL1;
    private string _spielerL2;

    public MainWindowViewModel()
    {
        SpielerL1 = System.IO.Directory.GetCurrentDirectory() + @"\Images\queen_of_clubs.png";
        SpielerL2 = System.IO.Directory.GetCurrentDirectory() + @"\Images\queen_of_diamonds.png";
    [...]
}

    public string SpielerL1
    {
        get { return _spielerL1; }
        private set
        {
            _spielerL1 = value;
            OnPropertyChanged("SpielerL1");
        }
    }

    public string SpielerL2
    {
        get { return _spielerL2; }
        private set
        {
            _spielerL2 = value;
            OnPropertyChanged("SpielerL2");
        }
    }
}

In my mainwindow view I am only instantiating the viewmodel and using the control with SourceLeft="{Binding SpielerL1}" and SourceRight="{Binding SpielerL2}"... 在我的主窗口视图中,我只是实例化viewmodel并使用SourceLeft =“{Binding SpielerL1}”和SourceRight =“{Binding SpielerL2}”的控件...

My control code behind looks like this (deleted sourceright to make it shorter): 我后面的控制代码看起来像这样(删除了sourceright以缩短它):

public partial class HandControl
{
    public HandControl()
    {
        InitializeComponent();
        DataContext = this;
    }

    public string SourceLeft 
    {
        get
        {
            return (string) GetValue(SourceLeftProperty);
        }
        set
        {
            SetValue(SourceLeftProperty, value);
        }
    }
    public static readonly DependencyProperty SourceLeftProperty = DependencyProperty.Register("SourceLeft", typeof(string), typeof(HandControl), new PropertyMetadata(""));
}

And finally my usercontrol xaml, which isnt recognising the datacontext or atleast not showing my images: 最后我的usercontrol xaml,它不识别datacontext或至少没有显示我的图像:

<UserControl x:Class="FoolMe.Gui.Controls.HandControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="3*" />
        </Grid.ColumnDefinitions>
        <Image Grid.Column="1" 
               Source="{Binding SourceLeft}" />
        <Image Grid.Row="0"
               Grid.Column="0"
               Grid.ColumnSpan="2" 
               Source="{Binding SourceRight}" />
    </Grid>
</UserControl>

Since I havent done much with WPF and usercontrols yet, I have no clue, whats wrong. 由于我还没有完成WPF和用户控制,我没有任何线索,什么是错的。 Without the usercontrol it has working fine, but outsourcing it like this, my window keeps "white". 没有用户控制它工作正常,但像这样外包,我的窗口保持“白色”。

Anyone got an idea, what went wrong? 任何人都有了想法,出了什么问题?

You shouldn't set the DataContext of the UserControl to itself. 您不应将UserControlDataContext设置为自身。 However, your real problem comes from your Binding on the Image elements. 但是,您真正的问题来自于Image元素上的Binding You should use a RelativeSource Binding instead: 您应该使用RelativeSource Binding

<Image Grid.Column="1" Source="{Binding SourceLeft, RelativeSource={RelativeSource 
    AncestorType={x:Type YourXmlNamespacePrefix:HandControl}}}" />
<Image Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Source="{Binding SourceRight, 
    RelativeSource={RelativeSource AncestorType={x:Type YourXmlNamespacePrefix:
    HandControl}}}" />

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

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