|
and by the way you said to transfer the logic to wait
do you suggest to use another thread except the ui thread?
|
|
|
|
|
Hi,
I have to create some ContentControl code behind and be able to change their content and their DataTemplate according to their data type (image or text)
(So, I've created a ContentControl code behind, given it (code-behind too) a style from the XAML. In this style I've set DataTemplates)
I'm blocked with a binding problem! The 'person' object is displaying but I can't get to its 'display_text' property!
Could you please help? Thanks!!
PS: By the way, my goal is to create containers dynamically, then send them a 'person' object and have a picture displayed if available or a text displayed (if image not available)...so, if you have other suggestions on how to achieve this they all are welcome!!
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2...l/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525" Background="#FF3A3A48">
<Window.Resources>
<local:RelativeToAbsolutePathConverter x:Key="relToAbsPathConverter" />
<DataTemplate x:Key="ccTexte">
<TextBlock Text="{Binding display_text}" HorizontalAlignment="Center" Foreground="Beige"/>
</DataTemplate>
<DataTemplate x:Key="ccImage">
<Image x:Name="img" Source="{Binding display_image, Converter={StaticResource relToAbsPathConverter}}"/>
</DataTemplate>
<Style x:Name="ccStyle" TargetType="ContentControl">
<Setter Property="ContentTemplate" Value="{StaticResource ccTexte}"/>
</Style>
</Window.Resources>
<Grid Name="mainGrid">
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="416,12,0,0" Name="Button1" VerticalAlignment="Top" Width="75" />
</Grid>
</Window>
Imports System.IO
Class MainWindow
Private Sub MainWindow_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded
mainGrid.Children.Add(designCC(300, 200, "01"))
Dim cc As ContentControl = LogicalTreeHelper.FindLogicalNode(mainGrid, "CC_01")
cc.Content = New person With {.display_image = "01.png", .display_text = "Text 01"}
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
Dim cc As ContentControl = LogicalTreeHelper.FindLogicalNode(mainGrid, "CC_01")
cc.Content = New person With {.display_image = "02.png", .display_text = "Text 02"}
End Sub
Private Function designCC(width As Integer, height As Integer, id As String) As ContentControl
Dim cc As New ContentControl
cc.Width = width
cc.Height = height
cc.Name = "CC_" & id
cc.Style = DirectCast(Me.Resources("ccStyle"), Style)
Return cc
End Function
End Class
Public Class person
Private _display_image As String
Public Property display_image() As String
Get
Return _display_image
End Get
Set(value As String)
_display_image = value
End Set
End Property
Private _display_text As String
Public Property display_text() As String
Get
Return _display_text
End Get
Set(value As String)
_display_text = value
End Set
End Property
End Class
Public Class RelativeToAbsolutePathConverter
Implements IValueConverter
Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert
Dim relative As [String] = TryCast(value, String)
If relative Is Nothing Or relative = "" Then
Return Nothing
End If
Return System.AppDomain.CurrentDomain.BaseDirectory & "Images\" & relative
End Function
Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
Throw New NotImplementedException()
End Function
End Class
|
|
|
|
|
I am currently using the SelectionChanged event in my ListView to fire when a row in the GridView is selected. Now I have ran into a problem that if the first row in the gridview is selected the SelectionChange will not fire. Can someone please help me determine how I detect if the first row is the row is selected?
modified 7-Sep-16 14:57pm.
|
|
|
|
|
I did find the ListView UnselectAll() function but even that did not work.
|
|
|
|
|
Has anyone ever seen this? I have a Listbox control:
<ListBox Grid.Row="3" Grid.ColumnSpan="3" Margin="3" SelectionMode="Multiple"
ItemsSource="{Binding Children}" SelectedItem="{Binding SelectedChild}" SelectionChanged="Children_SelectionChanged">
And there is a property IsSelected in the class that selecting an item changes correctly.
I also have a "Select All" button and an "Unselect All" button which seem to work. After pressing the button and counting the selected objects I get the expected counts.
Now my listbox has more items than fit in the view area, so the scroll bars appear.
The problem comes when I press the select all button and then scroll the list box. Even though my count said they were all selected, after I scroll, the items that were not in the window at the time the button was pressed revert to their prior state. The IsSelected property is re-called by the scrolling.
I don't know if I can make it any clearer. How do I keep the scroll bar from resetting the IsSelected property? Or How can I get the listbox to keep the items outside the visual area selected or unselected?
Thanks for any help.
Brent
|
|
|
|
|
This has always confused me and I would love to set the record straight.
Say you have this style:
<Style TargetType="{x:Type local:ControlBasedOnUserControl}">
<Setter Property="Width" Value="50" />
<Setter Property="Height" Value="50" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:ControlBasedOnUserControl}">
<Border>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Does WPF automatically apply the width and height of the control to the template's parent element or should the width and height be bound explicitly in the template?
<Style TargetType="{x:Type local:ControlBasedOnUserControl}">
<Setter Property="Width" Value="50" />
<Setter Property="Height" Value="50" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:ControlBasedOnUserControl}">
<Border Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I've found that I can set a width and height regardless if it is explicitly defined in template, but that doesn't seem right. Thoughts?
|
|
|
|
|
I have a Telerik RadCombobox where I need to react to the change of selected item and store the selected value.
<telerik:RadComboBox ItemsSource="{Binding CurrencyList}"
DisplayMemberPath="Currency"
SelectedValuePath="CurrencyID"
SelectedValue="{Binding SelectedIssuance.IssuanceCurrencyID, Mode=TwoWay}"
IsSynchronizedWithCurrentItem="True"
SelectedItem="{Binding SelectedCurrency,Mode=OneWayToSource}" />
This works the first time I access the view, however subsequent accesses on the same or another record the displayed value is always the first record in the CurrencyList
Any suggestions on how to fix this issue?
One idea is to get the changed value in the VM from the Model by subscribing to the OPC event and remove the SelectedItem binding.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I have been fighting with the OpenFileDialog to try to read the bytes of any type of file into a stream or byte[] to store in a database table but can't seem to get it right. Is using the OpenFileDialog class the correct way to go?
|
|
|
|
|
With the OpenFileDialog, the only property you're going to want to really use is the Filename. Once you have this, you would fall back to standard File operations to read the bytes (ReadAllBytes would be the one I would choose).
This space for rent
|
|
|
|
|
How find out size of "files" or each element(foreach (var file in files)) if files list? Could be quite big.
WPF serialization doesnt work in UWP.
var openPicker = new FileOpenPicker();
openPicker.FileTypeFilter.Add("*");
var files = await openPicker.PickMultipleFilesAsync();
modified 27-Aug-16 11:08am.
|
|
|
|
|
You use code similar to this:
foreach (StorageFile file in files)
{
BasicProperties properties = await file.GetBasicPropertiesAsync();
ulong size = properties.Size;
}
This space for rent
|
|
|
|
|
My problem is very difficult to describe but this is what I am trying to do. See all code below. What I have is a parent WPF Window that has a child View, created as a UserControl, added to the parent Window in it's markup. Now the problem is this. When a row, on the child UserControl View's GridView is clicked I want to close the child view's parent (Window) then navigate to an entirely new Window and open it. When I click the row I get the error 'Invalid cast exception". See below:
Static class defined in the same namespace:
public static class closeWindow
{
public static object WinObject;
public static void CloseParent()
{
using ((System.IDisposable)closeWindow.WinObject)
{
((Window)WinObject).Close();
}
}
}
Markup in the XAML parent Window page:
<v:savedraftview>
public partial class SavedDrafts : Window
{
public SavedDrafts()
{
InitializeComponent();
LoadMe();
}
public void loadMe()
{
closeWindow.WinObject = this;
}
}
Code in the Event contained the child view.
Intake1 window = new Intake1();
window.Show();
closeWindow.CloseParent();
modified 23-Aug-16 9:38am.
|
|
|
|
|
The fix was instead of using a Using() block of code I changed it to a try/catch:
try ((System.IDisposable)closeWindow.WinObject)
{
((Window)WinObject).Close();
}
catch(Exception e)
{
}
|
|
|
|
|
The Window class[^] does not implement IDisposable . Neither do any of it's base classes. Therefore, you cannot cast a Window to IDisposable .
Why have you declared the field as an object if it's only ever going to store a Window ? Just declare it as Window and avoid the cast.
Your CloseParent method needs to check whether the field has a value; otherwise, you could end up with a NullReferenceException .
You should probably set the field back to null once the window has been closed.
And you could save yourself a lot of trouble by avoiding the static class, and using the Window.GetWindow method[^] instead:
Intake1 window = new Intake1();
window.Show();
Window parentWindow = Window.GetWindow(this);
if (parentWindow != null) parentWindow.Close();
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I'm new to WPF/MVVM and have been trying to come up with a solution for communicating between ViewModels. The best i've been able to come up with is below, it works, but it feels dirty and shameful. Any suggestions would be greatly appreciated.
public delegate void OnMessageReceivedEventHandler(object sender, object message);
public static class Message
{
static List<OnMessageReceivedEventHandler> listeners = new List<OnMessageReceivedEventHandler>();
public static void AddListener(OnMessageReceivedEventHandler onMsgReceived)
{
listeners.Add(onMsgReceived);
}
public static void RemoveListener(OnMessageReceivedEventHandler onMsgReceived)
{
listeners.Remove(onMsgReceived);
}
public static void SendMessage(object sender, object msg)
{
foreach (OnMessageReceivedEventHandler m in listeners)
if (m != null)
m(sender, msg);
}
}
Nothing works better than just improving your product.
|
|
|
|
|
The generally accepted method for communicating between VMs is to use a Mediator or Messenger. If you take a look at MVVM Light, for instance, you can see how to write one and how to use it.
This space for rent
|
|
|
|
|
|
In addition to Pete's answer, you could also use Rx to communicate between viewmodels.
Sacha uses that here: Attached VM Behaviours[^]
|
|
|
|
|
Hello everybody,
I am struggling with the decision how to design my application. Let me describe my problem:
The application has a navigation on the left and the region to display the according view to the right.
It shall load projects and those projects can be of type A, B or C. The navigation is the same for all types, but some of the views differ.
My plan was for all type depending items in the navigation to have a general view which offers a region and the modules A, B and C which all provide a view for that region. During the loading of the project depending on its type the module / library A, B or C is loaded while the other library is unloaded. But I found out it is not supported to unload modules (only complete AppDomains).
I am developing in C# using VS 2010 and Prism 4.0.
Anyone has an idea how to best solve this issue? Is creating and unloading a temporary AppDomain really the best solution?
|
|
|
|
|
I have no idea of what your app is supposed to do, but in my "apps", I often simply toggle the visibility of a component instead of worrying about "loading" and "unloading" UI elements.
|
|
|
|
|
Thank you for your proposal Gerry, that is pretty much what I'll be doing now.
Each library checks if it is applicable for the current type and sets the view's visibility accordingly.
This does not really make me happy as I have to load all assemblies, but it is the best solution I can think of right now.
|
|
|
|
|
Glad that's working for you; sometimes the simplest solution is the best one.
If a better solution comes to mind later (as it often does), you have the option to go back and re-factor if it makes a difference. That's typical of software; when that gnawing feeling stops, you know you are "optimal".
|
|
|
|
|
I have a logon DLL that has a form which needs to be displayed in the main applications main windows navigation Frame. There is no mucking about with app domains but it is a PITA to put it together.
App.xaml - get the arguments passed into the start up exe and pass them to the main window VM.
MainVM has a reference to the main view (I know breaks the MVVM model) and the logon DLL. It gets the URL for the logon view from the DLL code>@"pack://application:,,,/LogonDLLName;component/Views/Logon.xaml and puts it into the frame in the main window.
It subscribes to the LoadCompleted method of the view to get the datacontext of the Logon view. And the PropertyChanged event in the Logon DLL VM.
PropertyChanged gets the content from the logon event and proceeds with the application.
As I said this is a PITA but it does work and we can relatively easily use the same logon DLL for all the applications. I hesitate to put this out there as I'm bloody certain there is a better way to do it.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
What occurs to me is that you can solve this using a combination of this article[^] with some clever work with the AppResources to register your views with "on-demand", replacing view references as necessary.
This space for rent
|
|
|
|
|
I have been posting about my SignalR project and about how I have been unable to connect to the SignalR server component on my server.
Here is a simple Sample SignalR Chat APp.
Run the app, start the server. Then open an additional Client and you'll see that it works fine.
I have then copied the server project's files to my Server, opened a port, disabled the firewall, and attempted to connect.
Depending on the things I try, I get different error messages, all telling me in some way that it could not find the server, or timed out, etc.
At this point I'm really desperate. I MUST get a SignalR server working on my Server. I'd be willing to pay to get help.
If anyone know how to do this, please help.
Thanks
If it's not broken, fix it until it is
|
|
|
|