小编Fra*_*nky的帖子

WPF:将变量从父xaml传递到usercontrol

我正在尝试将MainWindow.xaml中的int变量传递给UserControl.当我重新调试myGridSize总是等于零.我将不胜感激任何帮助.

MainWindow.xaml

x:Name="myWindow">
 <Grid>
        <my:SudokuGrid x:Name="mySudokuGrid" myGridSize="{Binding SudokuSize, ElementName=myWindow}">
        </my:SudokuGrid>
</Grid>
Run Code Online (Sandbox Code Playgroud)

MainWindow代码

    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();
    }
Run Code Online (Sandbox Code Playgroud)

UserControl.xaml

x:Name="gridCustom">
    <UniformGrid Name="SudokuUniGrid" Style="{StaticResource CustomGridStyle}">
    </UniformGrid>
Run Code Online (Sandbox Code Playgroud)

UserControl代码

    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); }
    } …
Run Code Online (Sandbox Code Playgroud)

c# wpf xaml binding user-controls

5
推荐指数
1
解决办法
5058
查看次数

标签 统计

binding ×1

c# ×1

user-controls ×1

wpf ×1

xaml ×1