首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在doubleclick上,window - dependency属性值中的WPF用户控件为空

在doubleclick上,window - dependency属性值中的WPF用户控件为空
EN

Stack Overflow用户
提问于 2018-07-31 15:44:54
回答 1查看 424关注 0票数 0

我已经在这里上传了我的示例项目:https://www.file-upload.net/download-13252079/WpfApp1.zip.html,这是一个包含多个相同用户控件的WPF窗口。UserControl有一个名为"Text“的依赖属性,该属性绑定到MainWindowViewModel的属性,并在用户控件的TextBlock中成功显示。但是,如果我双击UserControl并希望它给出其dependency属性的值,则该值为空。为什么会这样呢?非常感谢你的帮助!

编辑:对不起,这里有一些源代码:UserControl的XAML:

代码语言:javascript
运行
复制
<UserControl x:Class="WpfApp1.UserControl1"
         ...
         x:Name="UC1">    
    <StackPanel Orientation="Vertical">
        <TextBlock Margin="5" Text="Test" FontSize="20"></TextBlock>
        <TextBlock Margin="5" Text="{Binding ElementName=UC1, Path=Text}" FontSize="20"></TextBlock>
    </StackPanel>
</UserControl>

UserControl的代码:

代码语言:javascript
运行
复制
public partial class UserControl1 : UserControl, INotifyPropertyChanged
{
    public UserControl1()
    {
        InitializeComponent();
    }


    string text;
    public string Text
    {
        get { return text; }
        set { SetProperty(ref text, value); }
    }
    public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
        "Text", typeof(string), typeof(UserControl1));




    public event PropertyChangedEventHandler PropertyChanged;

    protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null)
    {
        if (Equals(storage, value))
        {
            return false;
        }

        storage = value;
        OnPropertyChanged(propertyName);
        return true;
    }

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

主窗口的XAML:

代码语言:javascript
运行
复制
<Window x:Class="WpfApp1.MainWindow"
    ...>
    <Window.DataContext>
        <local:MainWindowViewModel />
    </Window.DataContext>
    <StackPanel>
        <local:UserControl1 Text="{Binding Values[0]}"
                        MouseDoubleClick="UserControl1_MouseDoubleClick">    
        </local:UserControl1>
        <local:UserControl1 Text="{Binding Values[1]}"
                        MouseDoubleClick="UserControl1_MouseDoubleClick"> 
        </local:UserControl1>
        <local:UserControl1 Text="{Binding Values[2]}"
                        MouseDoubleClick="UserControl1_MouseDoubleClick"> 
        </local:UserControl1>
        <local:UserControl1 Text="{Binding Values[3]}"
                        MouseDoubleClick="UserControl1_MouseDoubleClick"> 
        </local:UserControl1>
    </StackPanel>
</Window>

主窗口的代码在后面:

代码语言:javascript
运行
复制
    private void UserControl1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        if (sender is UserControl1)
        {
            // why is (sender as UserControl1).Text null?
            MessageBox.Show("Text is: " + (sender as UserControl1).Text);
        }
    }

主窗口的视图模型:

代码语言:javascript
运行
复制
class MainWindowViewModel : BindableBase
{
    public MainWindowViewModel()
    {
        Values = new ObservableCollection<string>();
        Values.Add("first string");
        Values.Add("second string");
        Values.Add("third string");
        Values.Add("fourth string");
    }

    #region Values

    private ObservableCollection<string> values;
    public ObservableCollection<string> Values
    {
        get { return values; }
        set { SetProperty(ref values, value); }
    }

    #endregion
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-31 16:13:55

下面是你的UserControl后面的代码应该是什么样子。您不需要实现INotifyPropertyChanged。

有关所有细节,请参阅Custom Dependency Properties。具体地说,您必须从Text属性包装器的getter和setter调用GetValueSetValue (不能调用其他任何东西)。

代码语言:javascript
运行
复制
public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register(
            nameof(Text), typeof(string), typeof(UserControl1));

    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }
}

对于在其自身的XAML中绑定到UserControl的Text属性的绑定,您可以使用RelativeSource而不是ElementName来保存无用的生成的类成员:

代码语言:javascript
运行
复制
<UserControl x:Class="WpfApp1.UserControl1" ...>    
    ...
    <TextBlock Text="{Binding Text,
                      RelativeSource={RelativeSource AncestorType=UserControl}}" .../>
    ...
</UserControl>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51608025

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档