I have an xaml datagrid template in my WPF application that binds/pulls data from a sharepoint site. Each row has a button with a tooltip and the text is dynamically loaded from this connection. When a user clicked on the button, it would copy the tooltip text to the Windows ClipBoard. Just recently, my existing code behind stopped working, and can't seem to get that dynamically loaded tooltip text anymore to work with in code. I've ruled out the clipboard as I can manually set the text and it copies to the clipboard and I can still see the dynamically loaded text in the UI so it can't be the connection. Below is how I use to get the text in the click event:
var buttonTemplate = ((Button)sender).ToolTip;
var buttonTTtext = ((TextBlock)buttonTemplate).Text;
System.Windows.Clipboard.SetText(buttonTTtext.ToString());
Here is the xaml I'm using in the datagrid template:
<mui:ModernButton Click="someButton_Click" Tag="{Binding Path=MyTemplate}" >
<mui:ModernButton.ToolTip>
<TextBlock Text="{Binding Path=template}" TextWrapping="Wrap" />
</mui:ModernButton.ToolTip>
</mui:ModernButton>
Thanks for the help...the tooltip text was in the DataContext of the button. I solved my issue with this code:
Button btn = (Button)sender;
var dc = btn.DataContext.GetType().GetProperty("template").GetValue(btn.DataContext, null);
Related
I have a WPF DataGrid with a ColorPicker column. All works fine, but when the user try to click on the advanced button (see picture), the ColorPicker close.
When the user click again in the column, the ColorPicker opens and suddenly the "Advanced" button works. But when he click on a other column, it opens, he want click the advanced button, the ColorPicker close.
Here is the code of the DataTemplate in XAML file
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<xceed:ColorPicker SelectedColor="{Binding Color, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, Converter={StaticResource StringToWindowsMediaColorConverter}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
Any suggestions, how can I fix this issue? Thank you.
I had this problem too. I made the following workaround.
I bind the IsOpen Property of the ColorPicker to a new Property. Then I add a new column to the grid. In this column, I put a new button which pushes the IsOpen Property. I set the column with the ColorPicker on IsEnabled = false. So the only way to open the picker is the button-column. When the ColorPicker is openened by the button, it works normally and stays open until a color is clicked. Switching between the standard and advanced is no problem.
To create a new column to open the colorpicker is not nice, but I found no other solution.
This is a MVVM WPF Question., c#.
Within a Window I have a Tab control that looks like this
<TabControl TabStripPlacement="Top" >
<TabItem Style="{StaticResource Tabitemstyle}">
<TabItem.Header>
<Label Content="Home" Style="{StaticResource Tablablestyle}"/>
</TabItem.Header>
<v:HomePageView/>
</TabItem>
<TabItem ....
<v:OtherPageView/>
The trick is that there exists a textbox within the 2nd tab item that I wish to have input focus when the user selects the 2nd tab.
I've tried a few solutions, but the closest one so far (using data trigger style, or focused element) almost works:
I can see that the cursor is intended to be within the text box, but it doesn't blink. It seems the focus is still on the tab control in the outside window, not the text box element in the view that is defined by OtherPageView.xaml. When I hit tab once, it is all ok, but this is what I am trying to relieve the users of having to do.
I would use the code behind:
Listen to visibility changed event on the TabItem content (i.e., v:HomePageView)
Find the textbox UI element (you can simply give the textbox a name
in the xaml and refer to it from code behind)
Next, set focus on the text box using the UIElement.Focus() method
Finally if the keyboard did not focus , then use the
Keyboard.Focus(...) method to focus the keyboard on the textbox.
I have a chat window where each message is a TextBlock. I want to be able to select the text inside my TextBlocks. Google says to use a TextBox instead, which I cannot do because they do not support runs, which I am using to create hyperlinks inside my messages. What options do I have?
Check out the RichTextBox implementation for WPF.
How about making an CustomContentControl with TextBox (with selection style) and a Link in it and play with visibility based on content. Some thing like
<myControls:CustomContentControl>
<Grid>
// play with visibilty depending on content.
<CustomLink/>
<TextBox Style="{StaticResource SelectionStyle}"/>
</Grid>
</myControls:ContentControl>
I am using standard Visual Studio templates and I have a ItemsDetailPage that contains a FlipView with a RichTextBlock in its DataTemplate.
I want to set the RichTextBlock block to my custom Paragraphs generated in text. I think there is no way to bind RichTextBlocks Block in XAML so I am using code behind. In the Loaded event of RichTextBlock I set its Block, that works ok. But the problem is, the Loaded event gets called only once when the page is displayed. When I "flip" to another item, the selected item of the FlipView changes but the Loaded event does not get called again (I think this is ok).
I tried setting the RichTextBlock in the FlipViews SelectionChanged item but that does not work.
var ind = this.flipView.SelectedIndex;
var flipViewItem = this.flipView.ItemContainerGenerator.ContainerFromIndex(flipView.SelectedIndex);
if (flipViewItem != null)
{
var scroller = FindFirstElementInVisualTree<ScrollViewer>(flipViewItem);
var tb = scroller.FindDescendantByName("richTextColumns").FindDescendantByName("richTextBlock") as RichTextBlock;
SetRichContent(tb, (flipView.SelectedItem as ArticleViewModel).HtmlContent);
}
The SetRichContent gets called, sets the RichTextBlocks Blocks but visually they do not change and after a few flips, the whole app crashes without any additional information.
So my question is, how do I get my own code called on the RichTextBlock with each flip (seleced item change)?
You can bind rich text boxes. Make sure your data context is set properly. We need to see more code to make an appropriate answer.
<RichTextColumns>
<RichTextColumns.ColumnTemplate>
<DataTemplate>
<RichTextBlockOverflow Width="400" Margin="50,0,0,0"/>
</DataTemplate>
</RichTextColumns.ColumnTemplate>
<RichTextBlock Width="400">
<Paragraph>
<Run Text="{Binding Content}"/>
</Paragraph>
</RichTextBlock>
</RichTextColumns>
I have a scroll viewer that I want to display all of my user controls. I have two types of user controls which are education and work. They are just text boxes and a button. I dynamically create various numbers of each and add them to a list of type usercontrol. However I cannot get all of the user controls to display!
Scroll Viewer xaml
<ScrollViewer x:Name="myScrollViewer" HorizontalAlignment="Left" Height="518" Margin="128,10,0,0" Grid.Row="1" VerticalAlignment="Top" Width="1123" FontSize="22"/>
Code behind
List<UserControl> info = new List<UserControl>();
EducationControl educationControl = new EducationControl(school._school, school._degree, school._fieldOfStudy, school._educationStartDate, school._educationEndDate);
info.Add(educationControl);
WorkControl workControl = new WorkControl(work._employer, work._jobTitle, work._jobStartDate, work._jobEndDate);
info.Add(workControl);
myScrollViewer.Content = info;
When I set the content of the scrollviewer to info, it just displays a string like:
System.Collections.Generic.list'1[Windows.UI.Xaml.Controls.UserControl]
However the I can get an individual usercontrol to display properly when I do:
myScrollViewer.Content = info.ElementAt(0);
How can I display all of the user controls in the scroll viewer?
The ScrollViewer's content must be a list-style control, such as a ListBox. By itself, the ScrollViewer doesn't know how to render a List<UserControl>, so it just prints the type name.
It looks like you probably want items to become the ItemsSource of a ListBox or similar control.