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
Cannot use local variable 'TextBlock' before it is declared.
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". The TextBlock.Text
property you are trying to set inside the binding is getting confused with this variable name you have declared below it. 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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.