简体   繁体   English

如何在后面的代码中为同样在后面的代码中创建的文本块在后面的代码中创建数据绑定?

[英]How to create databinding in code behind for a textblock that is created also in code behind?

I have the following code : 我有以下代码:

private void Ok_Click(object sender, MouseButtonEventArgs e)
{
    MainWindow win = (MainWindow)Application.Current.MainWindow;
    int i = 1;  // counter for the name of each new textblock
    string name = String.Concat("sample", i);

    // add textblok to the document list of new samples

    if (File_name.Text != "")
    {
        TextBlock sampleText = new TextBlock();


        sampleText.Text = File_name.Text;
        sampleText.FontSize = 14;
        sampleText.FontFamily = new FontFamily("Sans-serif");
        sampleText.FontWeight = FontWeights.DemiBold;
        sampleText.Margin = new Thickness(20,0,0,0);
        sampleText.Name = name;
        sampleText.PreviewMouseDown += new MouseButtonEventHandler(test1);
        sampleText.Visibility = System.Windows.Visibility.Collapsed;

        //binding 

        Binding myBinding = new Binding();
        myBinding.Source = Application.Current;
        myBinding.Path = new PropertyPath("sampleName");
        myBinding.Mode = BindingMode.TwoWay;
        myBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        sampleText.SetBinding(, myBinding);


        Grid.SetColumn(sampleText, 0);

        win.sp_s.Children.Add(sampleText);

        // checking if the drop down of sample is already open,
        //if so it will show the last textblock with pressing the arrow button.
        var TextBlock = win.sp_s.Children.OfType<TextBlock>().FirstOrDefault();
        if (TextBlock.Visibility == System.Windows.Visibility.Visible)
        {
            sampleText.Visibility = System.Windows.Visibility.Visible;
        }
    }
    i += 1;  // increasing the loop of names by 1
    this.Close();
}

I want to set the target property of the SetBinding method to TextBlock.TextProperty but whenever I write TextBlock I get an error stating 我想将SetBinding方法的target属性设置为TextBlock.TextProperty但是每当我编写TextBlock ,都会出现错误提示

Cannot use local variable 'TextBlock' before it is declared. 声明之前不能使用局部变量'TextBlock'。

I suspect the issue is with this code - 我怀疑问题出在此代码-

var TextBlock = win.sp_s.Children.OfType<TextBlock>().FirstOrDefault();
        if (TextBlock.Visibility == System.Windows.Visibility.Visible)
        {
            sampleText.Visibility = System.Windows.Visibility.Visible;
        }

You have declared a variable name as "TextBlock". 您已将变量名称声明为“ TextBlock”。 The TextBlock.Text property you are trying to set inside the binding is getting confused with this variable name you have declared below it. 您试图在绑定内设置的TextBlock.Text属性与您在其下方声明的变量名称相混淆。 If you rename this variable the issue might get fixed. 如果重命名此变量,则问题可能会得到解决。

 var textblock = win.sp_s.Children.OfType<TextBlock>().FirstOrDefault();
 if (textblock.Visibility == System.Windows.Visibility.Visible)
     {
         sampleText.Visibility = System.Windows.Visibility.Visible;
     }

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

相关问题 如何使用Xaml中启动的同一对象在代码背后创建数据绑定? - How to create databinding in code behind using the same object that is initiated in xaml? 在后面的代码中将样式设置为文本块 - Set style to textblock in code behind 将DataTable绑定到DataGrid-背后的代码 - Databinding DataTable to DataGrid - Code Behind 如何在c#WPF中的代码后面为TextBlock设置TextOptions? - How to set TextOptions for a TextBlock in code behind in c# WPF? 如何通过代码隐藏将AutoGeneratedColumn标头包装在TextBlock中? - How to wrap AutoGeneratedColumn headers in a TextBlock from code-behind? 如何在后台代码中将Unicode字符设置为TextBlock的Text? - How to set Unicode character as Text of TextBlock in the code-behind? 如何在代码隐藏中从DataTemplate内部的TextBlock获取文本 - How to get the Text from TextBlock inside a DataTemplate in code-behind 在后面的代码中设置TextBlock文本将提供NullReferenceException - Setting TextBlock Text in Code Behind Gives NullReferenceException 如何从后台代码设置ItemsSource进行数据绑定 - How to set ItemsSource for a databinding from the code-behind 如何在XAML中“创建”的Code Behind中访问控件 - How to access control in Code Behind that was 'created' in XAML
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM