WPF:将变量从父 xaml 传递给用户控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37197906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF: Passing variables from parent xaml to usercontrol
提问by Franky
I'm trying to pass int variable from MainWindow.xaml to UserControl. When I'm debbuging myGridSize is always equal zero. I would appreciate any help.
我试图将 int 变量从 MainWindow.xaml 传递给 UserControl。当我调试时 myGridSize 总是等于零。我将不胜感激任何帮助。
MainWindow.xaml
主窗口.xaml
x:Name="myWindow">
<Grid>
<my:SudokuGrid x:Name="mySudokuGrid" myGridSize="{Binding SudokuSize, ElementName=myWindow}">
</my:SudokuGrid>
</Grid>
MainWindow code
主窗口代码
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
用户控件.xaml
x:Name="gridCustom">
<UniformGrid Name="SudokuUniGrid" Style="{StaticResource CustomGridStyle}">
</UniformGrid>
UserControl code
用户控制代码
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());
}
回答by d.moncada
A few issues.
几个问题。
Your dependency propertyneeds to be registered to the user controltype. Also you need to wait for the user controlto be fully loaded before accessing.
您dependency property需要注册到该user control类型。您还需要等待user control完全加载才能访问。
MainWindow.xaml
主窗口.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
主窗口.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(user control)
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(user control)
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
}
}

