private void UpdateButton_Click(object sender, RoutedEventArgs e) { // StaticResource 側 (左側) のスタイル設定を更新する // ※ Style は、適用された後では変更不可。 // 以下のようなコードは動かない。 //var ss = this.Resources["StaticStyle"] as Style; //var textSetter = ss.Setters.Cast<Setter>().First(s => s.Property.Name == "Text"); //textSetter.Value = this.Resources["Message"]; // ↑textSetter.Value は IsSealed=true になっていて、変更不可。 // 新たに Style オブジェクトを組み立てて、TextBlock にセットする // (XAML のスタイル定義と同じことを C# のコードで再実行する) var textSetter = new Setter(TextBlock.TextProperty, this.Resources["Message"]); var fontSizeSetter = new Setter(TextBlock.FontSizeProperty, this.Resources["RandomFontSize"]); var foregroundSetter = new Setter(TextBlock.ForegroundProperty, this.Resources["RandomColorBrush"]); var textBlockStyle = new Style(typeof(TextBlock), basedOn: this.Resources["TextBaseStyle"] as Style); textBlockStyle.Setters.Add(textSetter); textBlockStyle.Setters.Add(fontSizeSetter); textBlockStyle.Setters.Add(foregroundSetter); this.Resources["StaticStyle"] = textBlockStyle; TextBlock1.SetResourceReference(TextBlock.StyleProperty, "StaticStyle"); }
private void SetBinding() { Binding binding = new Binding() { ElementName = "Slider1", Path = new PropertyPath("Value"), Mode = BindingMode.OneWay }; Binding binding2 = new Binding() { ElementName = "myColor", Path = new PropertyPath("Text"), Mode = BindingMode.TwoWay, }; TextBlock1.SetBinding(TextBlock.FontSizeProperty, binding); TextBlock1.SetBinding(TextBlock.ForegroundProperty, binding2); }
public void openButton_Click(object sender, RoutedEventArgs e) { OpenFileDialog open = new OpenFileDialog(); open.DefaultExt = ".rtf"; open.Filter = "Rich Text files (*.rtf)|*.rtf|All files (*.*)|*.*"; if (textChanged == true) { toSave(); } System.Windows.Forms.DialogResult result = open.ShowDialog(); if (result == System.Windows.Forms.DialogResult.OK) { ClearText(); TextBlock1.AppendText(System.IO.File.ReadAllText(open.FileName)); textChanged = false; currentFileName = open.FileName; } }
private void OnTextBlockClearSelectionClicked(object sender, object args) { TextBlock1.Select(TextBlock1.ContentStart, TextBlock1.ContentStart); }
private void OnTextBlockSelectAllClicked(object sender, object args) { TextBlock1.SelectAll(); }
public void WriteToTextBlock(string message) { TextBlock1.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => { TextBlock1.AppendText(message + "\n"); })); }