|
Could you post your XAML that goes along with your view model code? From what it looks like here its all set up right on your code end.
|
|
|
|
|
Anyone know where I can find something like[^] this except for everything in WPF?
If it's not broken, fix it until it is
|
|
|
|
|
|
We have a MindFusion.RealTimeCharting control that has been extensively modified by us, so talking to them probably won't help.
On a tab control I have User Control, and in that is the WinForms chart control. We want to create a report from the UI. A "Report" is an image that's created and opened in Windows Photo Viewer.
For the purposes of reporting I've written a class called PrintLib that takes the element you want to print, creates an image of it, and opens the Windows Photo Viewer with the image displayed.
The problem is that the actual contents of the chart, a Sine Wave, doesn't show up. To correct this we added a function to the chart control called 'GetPrintBitmap' which returns a DrawingVisual. We then have to call this in code to produce an image file which is the used to replace the image of the XAML element at runtime.
What we would like is to have this happen when the element is rendered. Basically, we need to force WPF to draw the Sine Wave from the WinForms control when the XAML element image is created.
What I have now does work, except that I have to call a function in the WinForms control to get the bitmap back. Here's what I do have:
The process starts here. Element is the outer control containing the User Control, which contains the WinForms control:
public void Print(ItemsControl element)
{
ChartViewModel vm = ChartCollection[0];
RealTimeChart chart = vm.GetChart();
var dv = chart.GetPrintBitmap();
PrintLib.GenerateReport(element, dv);
}
Here is the significant portion of the GenerateReport method:
public static void GenerateReport(FrameworkElement element, DrawingVisual dv)
{
App.Current.Dispatcher.Invoke(() =>
{
var width = (int)element.ActualWidth;
var mainImageName = string.Empty;
if (dv == null)
{
mainImageName = CreateImageFromView(element, width);
}
else
{
mainImageName = DrawImage(dv, width, (int)element.ActualHeight);
}
.
.
.
});
}
private static string CreateImageFromView(FrameworkElement element, int width)
{
var rect = VisualTreeHelper.GetDescendantBounds(element);
var dv = new DrawingVisual();
using (var ctx = dv.RenderOpen())
{
var brush = new VisualBrush(element);
ctx.DrawRectangle(brush, null, new Rect(rect.Size));
}
var height = (int)element.ActualHeight;
if (height == 0)
{
height = (int)element.Height;
}
if (height == 0)
{
var message = "UI Element height is 0 and is not valid";
Debug.Print(message);
throw new ArgumentException(message);
}
return DrawImage(dv, width, height);
}
private static string DrawImage(DrawingVisual dv, int width, int height)
{
var rtb = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
rtb.Render(dv);
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(rtb));
var fileName = GetUniqueFileName("png");
using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
{
encoder.Save(fs);
}
return fileName;
}
So essentiially we don't want to have create the DrawingVisual from the WinForms control in code. Is it possible to get WPF to render the WinForms control's contents when it renders the rest of it?
Thanks
If it's not broken, fix it until it is
|
|
|
|
|
Based on your description, and WYSIWYG, I would just capture the Window's client area as an image.
Lots of C# code around to do that.
|
|
|
|
|
That's what my code already does, the problem is that the WinForms portion doesn't render as part of the WPF rendering process, so I'm calling a method on the WinForms control to return it's visible area as a bitmap, then replacing it ove the Winforms control.
I was hopeing there weas some way of forcing the WinForms control to render in the WPF rendering when the entire view is rendered.
If it's not broken, fix it until it is
|
|
|
|
|
There's a (free) WPF chart control; why do you need to use the "Windows Forms" control? (I didn't think that control was that special).
|
|
|
|
|
Hello Every One, I have a Critical Problem in my WPF Ink Canvas. The Problem is , when i draw something on it using mouse move , it draws easily , but when my application run on other system, which have 1280*720 screen Resolution, that time Ink canvas draws shape outside the drawing area and the shape is not visible also.
my development Machine screen Resolution - 1680*1050 and when i run this application on low level Resolution Machines then Ink canvas drawing area expand and shapes are draw outside the canvas , and they are not visible also. I already tried the viewbox control, but i am not getting which i want.
Please solve this Problem as soon as possible if anyone have the solution of It.
|
|
|
|
|
Use a
ScrollViewer control.
|
|
|
|
|
I have a UI with a MainWindow, which has a tab control. Some of those tabs have child tabs. So for Main Tab 1 there are 3 Child Tabs.
When the user selects Main Tab 1, Child Tab B, then clicks File->Print on the menu, how would you pass that menu command to the Active tab? Or somehow notify the ACTIVE TAB to print?
Thanks
If it's not broken, fix it until it is
|
|
|
|
|
Bind the TabControl.SelectedIndex
<telerik:RadTabControl Grid.Column="2" SelectedIndex="{Binding SelectedTab,Mode=TwoWay}">
Telerik has that property not sure if the standard TC does.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Did you even read the question?
What does binding the tab's SelectedIndex have to do with the Menu's Print command getting paassed all the way down the visual tree??
If it's not broken, fix it until it is
|
|
|
|
|
I assumed you are doing the printing from the VM, forgot you have your odd printing method.
Still I would do it the same anyway, tab changed is bound to the VM and sets the active tab, Print command tells a VM property for each tab to print. Seems like a complete kludge but then you have a creative printing method!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
If the TabControls are implemented as a List of Lists of TabControls (or similar) in your AppComposer file (or App.xaml.cs, wherever you defined them), you could just go through said list(s) and look for the TabControl.SelectedTab property.
Once you get hold of the active/selected tab that way, you could notify it to print.
Edit:
Just noticed that's about the same as what Mycroft suggested...but I would go for it as a first approach anyway
|
|
|
|
|
Does anyone know how to change the Spell Checking language of a WPF TextBox control to UK English.
I have tried setting Language="en-GB' and xml:lang="en-GB" as advised on all the websites - it does not work!
The software I am writing is literacy software (XAML Front end, C# Code Behind) for an education centre and needs to spell check in UK english
It seems really strange that WPF does not support UK English
- Windows does??
I have done this in a previous version of the software, but I think I implemented it as WinForms and maybe used a Word Interop for spell checking - but I cannot be sure that the users will have Word installed . . .
Please, Please, Help!
I read somewhere I can add a custom dictionary for UK English
anyone know where I can get one in .lex format
I have a file with all the differences between UK and US English - can I use this somehow
|
|
|
|
|
|
Hmm . . . thanks Muhammad, I tried that, it did not work for me, thanks for your answer
OK, I finally figured it out
I can set the default language once for the whole application using:
<i>// Set default language to UK English . . .</i>
Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.GetCultureInfo("en-GB");
FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement),
new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(Thread.CurrentThread.CurrentCulture.Name)));
After that, all TextBoxes and RichTextBoxes use UK English for SpellChecking
Many Thanks for all your help
|
|
|
|
|
I am trying to put a WPF Control in a winform for a project at work. I have done before for small controls and it worked well. I even put the DiagramDesignerPart1 in a winform as a practice run and did ok but some of the resources were missing and I did not notice or did not think it was a big deal.
Now I am trying to put the DiagramDesignerPart4 in a winform panel and the resources are missing at runtime.
The control shows all the resources at design time but when I run they are missing.
Also if I set the project to startup project that window runs fine and the resources all show up.
I have searched and searched for the answer and have not been able to get anything to work for me.
I did the following
1) I added the project to my project
2) Created a WPF Control and called it DiagramEditor and copied the xaml code into that to change it from a application to a control
3) I created a themes/generic.xaml file to put the info that was in app.xaml there
First I had a problem with the program crashing runtime using the static resources and the error was that it could not find the resources.
Then I changed to DynamicResources and it runs but none of the resources are showing up.
Generic.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DiagramDesigner">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/DiagramDesigner;component/Resources/Styles/Shared.xaml"/>
<ResourceDictionary Source="/DiagramDesigner;component/Resources/Styles/ToolBar.xaml"/>
<ResourceDictionary Source="/DiagramDesigner;component/Resources/Styles/GroupBox.xaml"/>
<ResourceDictionary Source="/DiagramDesigner;component/Resources/Styles/Expander.xaml"/>
<ResourceDictionary Source="/DiagramDesigner;component/Resources/Styles/ScrollBar.xaml"/>
<ResourceDictionary Source="/DiagramDesigner;component/Resources/ApplicationToolbar.xaml"/>
<ResourceDictionary Source="/DiagramDesigner;component/Resources/DesignerItem.xaml"/>
<ResourceDictionary Source="/DiagramDesigner;component/Resources/ToolboxItem.xaml"/>
<ResourceDictionary Source="/DiagramDesigner;component/Resources/Toolbox.xaml"/>
<ResourceDictionary Source="/DiagramDesigner;component/Resources/Connection.xaml"/>
<ResourceDictionary Source="/DiagramDesigner;component/Resources/Stencils/FlowChartStencils.xaml"/>
<ResourceDictionary Source="/DiagramDesigner;component/Resources/Stencils/ShapeStencils.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type local:DiagramEditor}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:DiagramEditor}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
DiagramEditor.xaml
<UserControl x:Class="DiagramDesigner.DiagramEditor"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:s="clr-namespace:DiagramDesigner"
xmlns:c="clr-namespace:DiagramDesigner.Controls"
mc:Ignorable="d"
d:DesignHeight="850" d:DesignWidth="1000">
<UserControl.Resources>
<ContextMenu x:Key="DesignerCanvasContextMenu">
<MenuItem Header="Paste" Command="{x:Static ApplicationCommands.Paste}">
<MenuItem.Icon>
<Image Source="/Resources/Images/Paste.png" Width="16"/>
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="Select All" Command="{x:Static s:DesignerCanvas.SelectAll}"/>
</ContextMenu>
</UserControl.Resources>
<Grid>
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ContentControl Content="{DynamicResource MyToolbar}"/>
<Grid Grid.Row="1" Margin="0,10,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="265"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0" Margin="0,0,5,0">
<Expander Header="Flow Chart" Content="{DynamicResource FlowChartStencils}" IsExpanded="True"/>
<Expander Header="Shapes" Content="{DynamicResource ShapeStencils}" IsExpanded="True"/>
</StackPanel>
<GridSplitter Focusable="False" Width="2" Background="LightGray"
VerticalAlignment="Stretch" HorizontalAlignment="Right"/>
<GroupBox Header="Diagram" Grid.Column="1" Margin="3,0,0,0">
<ScrollViewer HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto">
<s:DesignerCanvas Focusable="true" x:Name="MyDesigner"
Background="{DynamicResource WindowBackgroundBrush}"
Margin="10" FocusVisualStyle="{x:Null}"
ContextMenu="{DynamicResource DesignerCanvasContextMenu}"/>
</ScrollViewer>
</GroupBox>
</Grid>
</Grid>
</Grid>
</UserControl>
code to put on winform
System.Windows.Forms.Integration.ElementHost elementHost1 = new System.Windows.Forms.Integration.ElementHost();
DiagramEditor diagramEditor = new DiagramEditor();
private void LoadControls()
{
elementHost1.Dock = DockStyle.Fill;
elementHost1.BackColorTransparent = true;
elementHost1.Child = diagramEditor;
panel1.Controls.Add(elementHost1);
}
|
|
|
|
|
|
Thanks, I read in one of the many pages I looked at that we should have a generic file and the folder is created when we create a new custom control. I never saw this question in my list so I did not think it had posted, but I got the problem resolved by putting the resource links in The control resources and the window resources in those xmal files. I also had to change most of the static resources to dynamic resources. So far I just put the page on a panel in my winform app and did a little testing but the major revamp will come next week as I add stuff to it .
|
|
|
|
|
City Names City1 City2 City3 City4 City5 City6 City7 City8
City1 1 1 2 1 2 1 2 3
City2 1 1 2 1 2 1 1 2
City3 1 2 1 2 2 2 2 2
City4 2 1 2 1 2 2 2 1
City5 1 2 1 3 1 3 2 3
City6 2 2 2 2 3 1 1 2
City7 1 1 2 1 2 1 1 1
City8 2 1 3 1 3 2 1 1
I need to create this kind of Editable Datagrid Using MVVM if some one have this kind of data grid development experience please help me ..
(this numbers are distance between two city's it should be display inside of textbox component )
thanks..
|
|
|
|
|
|
Great link.
The first time I've seen someone admit that MVVM does NOT mean you NEVER have code behind.
He articulated what I always thought of as "display logic"; that every MVVM "fan" twists themselves into knots over trying to avoid.
|
|
|
|
|
It's well-written too. Hope you upvoted the article
|
|
|
|
|
Hi All,
I am using the following View and View Model in VB.Net, when I am trying to reference the View Model as DataContext
View is like this below
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"<br />
xmlns:empns="clr-namespace:TestWPFVBApplication.ViewModel"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<empns:EmployeeViewModel x:Name="_employeeViewModel" EmpId="1" EmpName="Abdul" EmpSalary="10000.00"
DeptId="1">
</empns:EmployeeViewModel>
</Window.DataContext>
<Grid>
</Grid>
</Window>
And my View Model is as below
Namespace TestWPFVBApplication.ViewModel
Public Class EmployeeViewModel
Implements INotifyPropertyChanged, IDataErrorInfo
Private _empId As Integer
Public Property EmpId() As Integer
Get
Return _empId
End Get
Set(ByVal value As Integer)
_empId = value
NotifyPropertyChanged("EmpId")
End Set
End Property
Private _empName As String
Public Property EmpName() As String
Get
Return _empName
End Get
Set(ByVal value As String)
_empName = value
NotifyPropertyChanged("EmpName")
End Set
End Property
Private _empSalary As Decimal
Public Property EmpSalary() As Decimal
Get
Return _empSalary
End Get
Set(ByVal value As Decimal)
_empSalary = value
NotifyPropertyChanged("EmpSalary")
End Set
End Property
Private _deptId As Integer
Public Property DeptId() As Integer
Get
Return _deptId
End Get
Set(ByVal value As Integer)
_deptId = value
NotifyPropertyChanged("DeptId")
End Set
End Property
Private _employees As ObservableCollection(Of Employee)
Public Property Employees() As ObservableCollection(Of Employee)
Get
Return _employees
End Get
Set(ByVal value As ObservableCollection(Of Employee))
_employees = value
NotifyPropertyChanged("Employees")
End Set
End Property
Private _error As String
Public ReadOnly Property [Error] As String Implements IDataErrorInfo.Error
Get
Return _error
End Get
End Property
Default Public ReadOnly Property Item(columnName As String) As String Implements IDataErrorInfo.Item
Get
If (columnName = "") Then
Return _error
End If
Return String.Empty
End Get
End Property
Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
Private Property CanExecute As Boolean
Private Sub NotifyPropertyChanged(propertyName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
Private _clickSaveCommand As ICommand
Public ReadOnly Property ClickSaveCommand() As ICommand
Get
Return If(_clickSaveCommand, (InlineAssignHelper(_clickSaveCommand, New SaveEmployeeCommand(Sub() AddAnItem(), CanExecute))))
End Get
End Property
Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
target = value
Return value
End Function
Private Sub AddAnItem()
_employees.Add(New Employee(_empId, _empName, _empSalary, _deptId))
End Sub
End Class
End Namespace
And I am trying to reference the View Model as below
xmlns:empns="clr-namespace: TestWPFVBApplication.ViewModel.EmployeeViewModel"
And DataContext as below
<Window.DataContext>
<empns:EmployeeViewModel x:Name="_employeeViewModel" EmpId="1" EmpName="Abdul" EmpSalary="10000.00"
DeptId="1">
</empns:EmployeeViewModel>
</Window.DataContext>
Then its giving me error, please help me any ideas or links or even suggestions would help me greatly.
Thanks in advance.
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
modified 26-May-16 19:08pm.
|
|
|
|
|