WPF:将变量从父 xaml 传递到用户控件

Posted

技术标签:

【中文标题】WPF:将变量从父 xaml 传递到用户控件【英文标题】:WPF: Passing variables from parent xaml to usercontrol 【发布时间】:2016-09-08 22:28:48 【问题描述】:

我正在尝试将 int 变量从 MainWindow.xaml 传递给 UserControl。当我调试时 myGridSize 总是等于零。我将不胜感激。

MainWindow.xaml

x:Name="myWindow">
 <Grid>
        <my:SudokuGrid x:Name="mySudokuGrid" myGridSize="Binding SudokuSize, ElementName=myWindow">
        </my:SudokuGrid>
</Grid>

主窗口代码

    public static readonly DependencyProperty SudokuSizeProperty =
        DependencyProperty.Register("SudokuSize", typeof(int), typeof(MainWindow), new FrameworkPropertyMetadata(null));
    private int SudokuSize
    
        get  return (int)GetValue(SudokuSizeProperty); 
        set  SetValue(SudokuSizeProperty, value); 
    

    public MainWindow()
    
        SudokuSize = 9;
        InitializeComponent();
    

UserControl.xaml

x:Name="gridCustom">
    <UniformGrid Name="SudokuUniGrid" Style="StaticResource CustomGridStyle">
    </UniformGrid>

用户控制代码

    public static readonly DependencyProperty myGridSizeProperty =
        DependencyProperty.Register("myGridSize", typeof(int), typeof(SudokuGrid), new FrameworkPropertyMetadata(null));

    public int myGridSize
    
        get  return (int)GetValue(myGridSizeProperty); 
        set  SetValue(myGridSizeProperty, value); 
    


    UniformGrid BuildGrid()
    
        //myGridSize = 9;           
        UniformGrid theGrid = new UniformGrid();
        for (int i = 0; i < myGridSize * myGridSize; ++i)
        
            myButton button = new myButton();
            button.cmLen = myGridSize;
            button.Width = 30;
            button.Height = 30;
            button.Tag = i;
            theGrid.Children.Add(button);            
        
        return theGrid;
    

    public SudokuGrid()
    
        InitializeComponent();
        SudokuUniGrid.Children.Add(BuildGrid());
    

【问题讨论】:

【参考方案1】:

一些问题。

您的dependency property 需要注册到user control 类型。另外,您需要等待user control 完全加载后才能访问。

MainWindow.xaml

<Window x:Class="WpfApplication5.MainWindow"
        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"
        xmlns:local="clr-namespace:WpfApplication5"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525"
        x:Name="myWindow">
    <Grid>
        <local:SudokuUniGrid myGridSize="Binding SudokuSize, ElementName=myWindow">
        </local:SudokuUniGrid>
    </Grid>
</Window>

MainWindow.xaml.cs

public partial class MainWindow : Window

    public static readonly DependencyProperty SudokuSizeProperty =
        DependencyProperty.Register("SudokuSize", typeof(int), typeof(MainWindow), new FrameworkPropertyMetadata(null));

    private int SudokuSize
    
        get  return (int)GetValue(SudokuSizeProperty); 
        set  SetValue(SudokuSizeProperty, value); 
    

    public MainWindow()
    
        SudokuSize = 9;
        InitializeComponent();
    

SudokiGrid.xaml(用户控件)

<UserControl x:Class="WpfApplication5.UserControl"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <UniformGrid x:Name="SudokuUniGrid">
    </UniformGrid>
</UserControl>

SudokiGrid.xaml.cs(用户控制)

public class SudokuUniGrid : UserControl

    public static readonly DependencyProperty myGridSizeProperty =
        DependencyProperty.Register("myGridSize", typeof(int), typeof(UserControl), new FrameworkPropertyMetadata(null));

    public int myGridSize
    
        get  return (int)GetValue(myGridSizeProperty); 
        set  SetValue(myGridSizeProperty, value); 
    

    public SudokuUniGrid()
    
        InitializeComponent();

        Loaded += SudokuUniGrid_Loaded;
    

    private void SudokuUniGrid_Loaded(object sender, RoutedEventArgs e)
    
        Console.WriteLine(myGridSize);

        // build here
    

【讨论】:

您的解决方案正在运行。现在我明白了什么是真正的问题。非常感谢。

以上是关于WPF:将变量从父 xaml 传递到用户控件的主要内容,如果未能解决你的问题,请参考以下文章

WPF应用程序中,调用用户控件时,可以访问到在这个用户控件Xaml代码中的所有内容,这样岂不是不安全?

VS2012 - 将WPF现有用户控件添加到项目中

WPF 将对象从窗口的用户控件传递到另一个窗口的用户控件

wpf 带参数的用户控件,如何在xaml中引入

wpf中用户控件更改期间的动画

将 ErrorTemplate 添加到 WPF 用户控件会禁用 TextBox 输入