Bind RichTextBox Document to Embedded Text File Without Use of Code-Behind - c#

I currently have a large license file embedded in my program that I'd like to bind a RichTextBox to. I've tried multiple methods but found no good way to do this without including something in the code-behind. I'd prefer to bind the document in the XAML itself. As an analogue, I currently have an image bound like so:
<Image Height="25" HorizontalAlignment="Left" Margin="12,12,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="23" Source="Resources\68PVb9k.png" />
Is there any way to achieve this with a RichTextBox document? If I attempt to bind the document directly I get an error:
"A 'Binding' cannot be set on the 'Document' property of type 'RichTextBox'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject."
So something like:
<RichTextBox Name="richTextBox1" Document="{Binding Path=Resources\InternalLicense.txt}" IsEnabled="False"/>
Won't work. (I realize the obvious formatting issues there. It's just an example)

TextBox IsReadOnly = true
TextBoxBase.IsReadOnly Property

Related

How to bind an automatically-set parameter in WPF with MVVM

So here's the situation:
I have a label (custom control derviative of it, if it matters), and I need to get its width and height with MVVM. However, if I set either of the parameters to {Binding XXX}, they are no long Auto and thus when I change the font in runtime their size doesn't update.
I read about using ActualWidth/Height, which sounds like just what I need besides the fact that it's not a dependence parameter, thus it seemss like I'd need to break MVVM for it.
Are there any better solutions?
EDIT
Well, the element in XAML looks nothing special. Just a million of bindings.
<local:DraggableLabel
Content="123"
Margin="{Binding Label2Position, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FontFamily="{Binding Label2Font.Family}"
FontSize="{Binding Label2Font.Size}"
FontWeight="{Binding Label2Font.Weight}"
FontStyle="{Binding Label2Font.Style}"
Foreground="{Binding Path=Label2Color,
UpdateSourceTrigger=PropertyChanged,
Converter={StaticResource ColorToBrushConverter}
}"/>
The default is,
Width="Auto"
which doesn't have to be written explicits, but can (changes nothing). It makes it resize when the font changes. If I set it to
Width="Binding {Label1Width}"
The binding works fine, but I no longer get the auto-adjustment.
Okay, so I found a workaround.
Since I had create a custom control, I was able to create a new DependencyProperty called RealWidth, then I added a OnSizeChanged event that updates it with the value of ActualWidth that you CAN get from inside the element every time the size of the element changes.

How to set and get tag in xaml dynamically?

While creating GUI using xaml, I created a textbox with a tag like this:
<TextBox Name="TextBox" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="216,178,143,120" Width="158"
Tag="myTag"/>
Now I want to let the user be able to change this tag. For that, I am looking for a kind of function of form:
TextBox.SetTag( "User Provided Tag" )
So that the tag can be changed into this one:
<TextBox Name="TextBox" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="216,178,143,120" Width="158"
Tag="User Provided Tag"/>
After searching the internet for quite a while, I didn't come up with any practical solution. Could someone help? Thanks.
You can use a binding between two controls. Say a user is allowed to enter a tag value from a TextBox. You just bind the Tag of the second TextBox to a Text property of the first TextBox:
<TextBox Name="enterTagTextBox" />
<TextBox Name="getTagTextBox" Tag="{Binding ElementName=enterTagTextBox, Path=Text}"/>
To test it I added a Button in my XAML:
<Button Height="25" Click="Button_Click_1"/>
In code behind I just retrieve the tag value and display it like this:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
string text = this.getTagTextBox.Tag.ToString();
global::System.Windows.Forms.MessageBox.Show(text);
}
I think your problem may arise from you naming your TextBox, "TextBox". Try giving it a name that does not clash with the class name, like "txtMyTextBox".
Then you can do, txtMyTextBox.Tag = "User Provided Tag";.
Or you can bind to it as PiotrWolkowski suggests.
However, I would like to add, that it seems like there should be a cleaner way of achieving your desired behaviour. With the caveat that I don't know the details of what you are trying to implement.
I strongly suggest creating a ViewModel (see: MVVM Pattern) to hold the data you want users to be able to edit. Then use the bindings in WPF to display the data.
You would need to use the property element usage in order to set the Tag property in Extensible Application Markup Language (XAML) to anything other than an object with a known and built-in type converter, such as a string.

Passing data between pages wp7

I have a little question.
I have a data template like that :
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Key="DangersItemTemplate">
<Grid Height="102" Width="447" Tap="Grid_Tap">
<Image Name="image" Source="{Binding Image}" HorizontalAlignment="Left" Width="90" />
<TextBlock Name="text" Text="{Binding Nom}" TextWrapping="Wrap" Margin="102,16,16,22"/>
</Grid>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
I want to use the Textlock control in the CSharp code to pass the Text property to the second xaml page, but I can't find the name of the control, it's inaccessible.
Any idea ?
Your binding the Text of the TextBlock so you must have the value in your datacontext. I'd add this value to the Querystirng and navigate to the page.
var text = ((Whatever) DataContext).Nom;
string page2Uri = string.Foramt("/PAge2.xaml?Nom={0}", text);
NavigationService.Navigate(new Uri(page2Uri, UriKind.Relative));
And then to get the data out of the querystring you can use the OnNavigatedTo method in your page, and then inspect this.NavigationContext.QueryString["nom"]
Or have a look into something like the MVVM pattern.
I just use VB.Net but I am sure you can convert.
Just use any shared variable in the application.
Here you can see it in an SourceCode example:
http://www.activevb.de/rubriken/kolumne/kol_30/res/nachtscanner.zip
Just use Public Shared MyText As String
in Application Class. This vairable you can access anywhere.
You can also use INotifyPropertyChanged for that shared property to bind.
Alternatively you can use Navigation Query. Read more here:
How can I pass query string variables with NavigationService.Navigate?

Binding keyword refers to what?

In this example:
<TextBox Name="textBox1" Height="23" Text="some text" HorizontalAlignment="Left" Margin="69,12,0,0" VerticalAlignment="Top" Width="120" />
<Label Content="{Binding Path=Text, ElementName=textBox1}" Height="28" HorizontalAlignment="Left" Margin="235,12,0,0" Name="label1" VerticalAlignment="Top" />
whatever I type on the textbox will be displayed on the label. I am learning xaml and I believe I understand most of the Bindings such as this one. But I think even though I understand what is happening, it will be nice to understand what the Binding keyword refers to? for example I can have:
<ListView ItemsSource="{Binding}" Name="lv1" />
I am having a hard time understanding what binding actually refers to? There is nothing after the Binding keyword, so what is it binding to? Does it refer to that control? Is it similar to the this keyword used in c# where it will refer to the current instantiated object of a class?
It's not a keyword, it's a markup extension.
Bindings always bind to the Path relative to the source, if there is no path they bind directly to the source, possible sources are the DataContext which is used implicitly if no other source is specified and ElementName, Source and RelativeSource. Please read the overview if you have not done so yet.
This is a binding to the Property DataContext of the window. If there is a list of elements in
that, you will see it in the ListBox.
For example type DataContext = new List<string> { "A", "B", "C" }; in the constructor of your window after InitializeComponents(); and you will see that list in your ListBox.

datatemplate in silverlight c#.net

i am creating a datatemplate for items in listbox and loading it using
(DataTemplate)XamlReader.Load(template), where template is
string template = String.Concat(#
"<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
<Border BorderBrush='#334873' BorderThickness='1,1,1,1' Width='450'>
<TextBox Height='72' HorizontalAlignment='Left' Margin='10,10,0,0' TextChanged="OnTextChanged" VerticalAlignment="Top" Width="460" />"
...................
"</Border></DataTemplate>");
i m getting error because of "OnTextChanged" event registerd.
I want to register a event in the template code.
How to do it?
To give you an answer: as you are using a Template it's rather difficult to map an event this way because the template doesn't know where it's put into. So OnTextChanged doesn't mean a thing in this context.
You should consider bindings instead. As you allready use a DataTemplate the usual story would be to Bindd the TextBox-Text property to some model-property
<TextBox Height='72' HorizontalAlignment='Left' Margin='10,10,0,0' Text="{Binding MyTextProperty}" VerticalAlignment="Top" Width="460" />"
Of course the DataContext here should have a property MyTextProperty but without your code (where you use the template) I can give no further details.

Categories

Resources