I have a RichTextBox that the user can edit to create a hyperlink (in my case to another page with the document rather than an external URL). Having successfully created the link I now need to be able to remove it.
I have code that identifies that I've got a hyperlink in the current selection:
TextSelection linkText = richTextBox.Selection;
if (linkText != null && !string.IsNullOrWhiteSpace(linkText.Text))
{
XElement root = XElement.Parse(linkText.Xaml);
XNamespace ns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
XElement linkElement = root.Element(ns + "Paragraph").Element(ns + "Hyperlink");
if (linkElement != null)
{
// Get here if have a Hyperlink. How do I remove or update?
}
}
However, I'm now stuck on the bit that goes inside the if test. How do I find the hyperlink so I can remove it completely?
My code for setting up the hyperlink is:
TextSelection linkText = richTextBox.Selection;
var hyperlink = new Hyperlink();
hyperlink.Inlines.Add(linkText.Text);
if (!String.IsNullOrEmpty(selectedTopic)) // A string holding the link target
{
// Setup hyperlink here
}
linkText.Insert(hyperlink);
I've managed to work out how to update the hyperlink:
foreach (var block in richTextBox.Blocks)
{
Paragraph p = block as Paragraph;
foreach (var inline in p.Inlines)
{
var hyperlink = inline as Hyperlink;
if (hyperlink != null && hyperlink.NavigateUri.AbsoluteUri.Contains(currentLink))
{
hyperlink.NavigateUri = new Uri(newLink);
}
}
}
I could use the same approach to delete the hyperlink, but how do I convert the Hyperlink to a normal Inline?
Looks like you are getting close you just need to hold a reference to the link then use Remove. Something like the following (I like to use Linq to make things a little more succinct):-
foreach (var p in richTextBox.Blocks.OfType<Paragraph>())
{
var hyperlink = p.Inlines.OfType<HyperLink>()
.FirstOrDefault(hl => hl.NavigateUri.AbsoluteUri.Contains(currentLink));
if (hyperlink != null)
{
p.Inlines.Remove(hyperlink);
break;
}
}
Edit: Want to leave the content of hyperlink in place? (i.e., just remove the wrapping hyperlink),
foreach (var p in richTextBox.Blocks.OfType<Paragraph>())
{
var hyperlink = p.Inlines.OfType<HyperLink>()
.FirstOrDefault(hl => hl.NavigateUri.AbsoluteUri.Contains(currentLink));
if (hyperlink != null)
{
int index = p.Inlines.IndexOf(hyperlink);
Span span = new Span();
foreach (var inline in hyperlink.Inlines.ToArray())
{
hyperlink.Inlines.Remove(inline);
span.Inlines.Add(inline);
}
// You may need code here to preserve the Font properties etc from hyperlink to span.
p.Inlines[index] = span;
break;
}
}
Related
I have a XtraGrid bound to a Datasource, where I want a single column to have a Check Box in it, and have a caption next to the Check Box in the same cell. I can get the Checked True or False from another column in the Datasource, and also I need to get the caption text from another column in the Datasource...That part is easy, once I get a handle on implementing the caption changes.
My question is:
How do I programmatically change the text that is displayed next to the CheckEdit in the same cell?
Similar to (Tick and Untick represent the check box state):
Tick Apples
Untick Bananas
However, my attempts only display the text 'Check' - obviously the default caption in the CheckEdit editor:
Tick Check
Untick Check
I have searched the DevExpress support centre and they only have instructions for the CheckEdit Repository item when used in the TreeList (not the XtraGrid). I understand it will involve managing the CustomDrawCell event and some others - which is fine.
I think, your instructions for XtraTreeList can be also applied to XtraGrid. But I can suggest to you the other way.
You can use GridView.CustomRowCellEdit event and two ResositoryItemCheckEdit objects:
private RepositoryItemCheckEdit editTick;
private RepositoryItemCheckEdit editUntick;
Just set your Tick text to editTick.Caption property and Untick text to editUntick.Caption property.
Here is example:
//Initialize repository items:
editTick = new RepositoryItemCheckEdit() { GlyphAlignment = HorzAlignment.Near, Caption = "Apples" };
editUntick = new RepositoryItemCheckEdit() { GlyphAlignment = HorzAlignment.Near, Caption = "Bananas" };
//Add handler for CheckedChanged event:
Action<object, EventArgs> action = (s, e) =>
{
var ownerEdit = s as CheckEdit;
if (ownerEdit == null)
return;
ownerEdit.Text = ownerEdit.Checked ? editTick.Caption : editUntick.Caption;
};
editTick.CheckedChanged += new EventHandler(action);
editUntick.CheckedChanged += new EventHandler(action);
//Some sample DataSource:
var table = new DataTable();
table.Columns.Add("ID", typeof(int));
table.Columns.Add("Bool", typeof(bool));
table.Rows.Add(0, false);
table.Rows.Add(1, false);
table.Rows.Add(2, true);
table.Rows.Add(3, false);
table.Rows.Add(4, true);
table.Rows.Add(5, true);
table.Rows.Add(6, false);
table.Rows.Add(7, true);
gridControl.DataSource = table;
//Here comes the CustomRowCellEdit:
gridView1.CustomRowCellEdit += (s, e) =>
{
if (e.Column.FieldName != "Bool" || e.CellValue == null) //Put your own field name here instead of "Bool".
return;
e.RepositoryItem = (bool)e.CellValue ? editTick : editUntick;
};
//Add CustomColumnDisplayText event, so you can see your "Apples" in group rows and filters:
gridView1.CustomColumnDisplayText += (s, e) =>
{
if (e.Column.FieldName != "Bool")
return;
e.DisplayText = (bool)e.Value ? editTick.Caption : editUntick.Caption;
};
is it possible to render a specific page in a razor function. I tried #RenderPage but i cant figure out the path. Are there any built in functions to accomplish this?
Thanks Johan
Not really a C1 specific approach, but personally my best approach has been to just make a separate web-request for the page in question, parse out the html and render it.
This code can serve as an example, its a 1:1 of what i'm using. As you can see the trick is to find the element that wraps your content, in my example its the element inside that has an id equals to ContentColumnInner
public static string GetContentFromPage(Guid pageId)
{
var DomainName = HttpContext.Current.Request.Url.Authority;
var Uri = String.Format("http://{0}/page({1})", DomainName, pageId);
var request = WebRequest.Create(Uri);
// If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials;
// Get the response.
using (var response = (HttpWebResponse)request.GetResponseWithoutException())
{
if (response.StatusCode != HttpStatusCode.OK)
{
LogError("StatusCode: " + response.StatusCode);
return null;
}
// Get the stream containing content returned by the server.
using (var responseStream = response.GetResponseStream())
{
if (responseStream == null)
{
LogError("ResponseStream is null");
return null;
}
// Open the stream using a StreamReader for easy access.
using (var stream = new StreamReader(responseStream))
{
// Read the content.
var responseFromServer = stream.ReadToEnd();
var beforeBodyStartIndex = responseFromServer.IndexOf("<body", StringComparison.Ordinal);
var afterBodyEndIndex = responseFromServer.LastIndexOf("</body>", StringComparison.Ordinal) + 7;
var body = responseFromServer.Substring(beforeBodyStartIndex, afterBodyEndIndex - beforeBodyStartIndex);
try
{
var xmlDocument = XElement.Parse(body);
var content = xmlDocument.Descendants().FirstOrDefault(o => o.Attribute("id") != null && o.Attribute("id").Value.EndsWith("ContentColumnInner"));
if (content == null || !content.HasElements)
{
return null;
}
var reader = content.CreateReader();
reader.MoveToContent();
return reader.ReadInnerXml();
}
catch (XmlException ex)
{
LogError("Error parsing xml: " + ex.Message);
return null;
}
}
}
}
}
I am using the WPF toolkit for its WatermarkTextBox control.
My application dynamically concatenates text strings and placeholders for a user to fill in the blanks.
foreach(var e in elements)
{
if (isText)
{
LetterText.Inlines.Add(new Run
{
Text = e,
BaselineAlignment = BaselineAlignment.Center
});
}
else
{
LetterText.Inlines.Add(new WatermarkTextBox
{
Watermark = e
});
}
isText = !isText;
}
This works quite well, but my problem occurs when I want to reassemble the text:
foreach(var inline in LetterText.Inlines)
{
if (inline.GetType() == typeof(Run))
{
sb.Append(((Run)inline).Text);
}
else if (inline.GetType() == typeof(WatermarkTextBox))
{
var wtb = inline as WatermarkTextBox;
sb.Append(wtb.Text);
}
}
This fails at compile time with 'Cannot convert Inline to WatermarkTextBox' (without the else clause, the conversion from Inline to Run works fine).
How can I get the text from the WatermarkTextBox?
This did the trick:
if (inline.GetType() == typeof(Run))
{
sb.Append(((Run)inline).Text);
}
else if (inline.GetType() == typeof(InlineUIContainer))
{
var container = inline as InlineUIContainer;
var wtb = container.Child as WatermarkTextBox;
if (wtb !=null)
sb.Append(wtb.Text);
}
I don't know why the InlineUIContainer does not also get implicitly created for Run elements
Hi guys im trying to make my textbox to view names on each item i click on my dropdownbox. So I would like to match the result with the dropdown and then display it in the textbox.
I have done this and it seems not working.
var sql = (from query in dwe.VW_CustumAddress
where query.Postal == DropDownListPostal.SelectedItem.ToString()
select query);
if (sql != null)
{
NavnBox.Text = data.Name;
}
try to debug this one.
var selectedItem = DropDownListPostal.SelectedItem;
if (selectedItem != null) {
var postal = selectedItem.ToString();//check if it looks like something you want
var data = dwe.VW_CustumAddress.FirstOrDefault(m => m.Postal == postal);
if (data != null)
NavnBox.Text = data.Name;
}
I have a SQL column as an int data type.
I'm trying to update en entry using Entity Framework, setting it's value to 0 but it does not get updated. I also don't get an error when Submitting Changes... Odd. Well I changed the Tag Name and set the count to 0, the name got updated but the count was not modified
Any ideas?
Thanks in advance
Code Snipet:
Add Tag - up the tag count:
Tag tag = _tagsRepository.GetTagByName(TagName);
if (tag == null)
{
tag = new Tag();
tag.CreateDate = DateTime.Now;
tag.Name = TagName;
tag.Count = 1;
}
else
{
tag.Count += 1;
}
tag = _tagsRepository.SaveTag(tag);
Remove tag from an item, update the tag count
Tag tag = _tagsRepository.GetTagByName(TagName);
if (tag != null)
{
tag.Count -= 1;
}
tag = _tagsRepository.SaveTag(tag);
GetTagByName Method
public Tag GetTagByName(string Name)
{
Tag result = null;
using (ISADataContext dc = conn.GetContext())
{
result = dc.Tags.Where(t => t.Name == Name).FirstOrDefault();
}
return result;
}
SaveTag Method
public Tag SaveTag(Tag tag)
{
using (ISADataContext dc = conn.GetContext())
{
if (tag.TagID > 0)
{
dc.Tags.Attach(new Tag { TagID = tag.TagID });
dc.Tags.ApplyCurrentValues(tag);
}
else
{
dc.Tags.AddObject(tag);
}
dc.SaveChanges();
}
return tag;
}
Workaround:
using (ISADataContext dc = conn.GetContext())
{
if (tag.TagID > 0)
{
if (tag.Count == 0)
{
Tag t = dc.Tags.Where( tt=> tt.TagID == tag.TagID).First();
t.Count = 0;
}
else
{
dc.Tags.Attach(new Tag { TagID = tag.TagID });
dc.Tags.ApplyCurrentValues(tag);
}
}
else
{
dc.Tags.AddObject(tag);
}
dc.SaveChanges();
}
return tag;
You shouldn't wrap your DataContext in using statements in your scenario. Doing this means that you're retrieving the object in one context and trying to save the existing object in a new context. This means that when you're trying to save in the second context, it has no idea that the object already exists in the database.
I ran into the same problem as yours - every other value except 0 works fine. Can we get some solution to this?
EDIT: After some research, I found a way to fix this. You have to modify this line:
dc.Tags.Attach(new Tag { TagID = tag.TagID });
In the stub object, add the problematic property (in your case Count) and set its value to something that is never possible in the db, for example -2. In this way, the EF context will also compare the count value of the attached object and will always know there is a difference. So your line becomes:
dc.Tags.Attach(new Tag { TagID = tag.TagID, Count = -2 });
I am not sure if this will work every time, but for now it seems to do the job.