Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all 18858 articles
Browse latest View live

Enable Mouse Events over Nested Datagrid WPF

$
0
0

How to enable mouse events over a WPF nested datagrid?

where this nested datagrid is collapsed and becomes visible after pressing a button.

Thanks,


UIElement.Measure do not include the size of any applied Effects (eg DropShadowEffect)

$
0
0

I've created a custom WPF control extending Canvas. The control exposes an ItemTemplate property so users (developers) can design their own look of the items being displayed. MeasureOverride and ArrangeOverride are (more or less) successfully implemented as well.

Inside my MeasureOverride I call Measure() on each and every element, and it sets the correct DesiredSize according to the user's ItemTemplate content. But how can I determine the size of any effects applied to the content?

e.g., this grid:

<Grid Width="200" Height="50">
  <Grid.Effect><DropShadowEffect ShadowDepth="10" Direction="0"></Grid.Effect>
</Grid.Width>

The DesiredSize will be set to 200x50 using the Measure method. But the real size will be 210x50 with the shadow included. Shouldnt my control and my MeasureOverride implementation actually report back 210x50 in this scenario?


Event Handlers of a mouse click

$
0
0
In the simplest way possible please tell me how to do an event handler for a mouse click.
        private int clickCounter = 0;
        private void MyCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (Device1RB.IsChecked == true)
            {
                clickCounter++;
                return;
            }
            else if (RemoveRB.IsChecked == true)
            {
                clickCounter--;
                return;
            }
            else
            {
                return;
            }
        }
        private void Switch_class()
        {
            switch (clickCounter)
            {
                case 1:
                    //code here
                    break;
                case 2:
                    //code here
                    break;
                case 3:
                    //code here
                    break;
                case 4:
                    //code here
                    break;
                case 5:
                    //code here
                    break;
                default:
                    //code here??
                    break;
            }
        }

This is what I have and I apparently need an event handler for the mouse click. Also if there is an easier way to do what I want then please point me in the right direction.

Nested UserControl

$
0
0

Hello Everyone

I'm struggling to solve the problem with my nested UserControl.

When I run the application the main window opens, inside the main window is a button, when I click that button it calls up a UserControl (called Applic).

Inside the Applic UserControl is another nested UserControl called PasswordScreen.

Within the PasswordScreen there is a button called Login, when I press this button to call another UserControl (called SwitchboardView) it is not firing nothing (here is the sample code)

private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            Applic ap = new Applic();
            SwitchboardView sbv = new SwitchboardView();

            ap.MainGridApplic.Children.Add(sbv);

        }

Could someone please help me solve this problem, I have been struggling for a long time to solve this problem but I just can't get it around.

Thanks in advance...

Kind regards

Roni


Roni Jata

Problem with radioButton(s) and image controls on window in WPF

$
0
0

Hi, guys and developers!

Please, help me with radioButton(s) and image control in WPF.

After creating new Window1 in my WPF project I add 18 radioButton and 1 image control to project.In project folder I have Image folder with 18 .png image.

How to make so that by pressing on radioButton1 in image booted 1.png from a Image folder, by pressing on radioButton2 booted 2.png and so on...

Help to solve this problem.

P.S.Sorry for my bad English :)

Thanx,

Bogdan.


Sorry for my bad English because I am from the Ukraine and I drink Vodka and it Fat :)

Issue hiding WPF FrameworkElement from the Visual Studio Toolbox

$
0
0

Hello,

I created a WPF custom control library with some custom classes derived from FrameworkElement and I want to hide these classes from the Visual Studio Toolbox, but I get different issues in VS2010 and VS2013.

Here below the classes of my custom control library:

[DesignTimeVisible(false)]
public class PersonObject : FrameworkElement
{
    private string _Name = "";
    private int _Age = 0;
    private Brush _ShirtColor = Brushes.Blue;

    public string Name
    {
        get { return _Name; }
        set { _Name = value; }
    }

    public int Age
    {
        get { return _Age; }
        set { _Age = value; }
    }

    public Brush ShirtColor
    {
        get { return _ShirtColor; }
        set { _ShirtColor = value; }
    }
}

[ToolboxBrowsable(false), TypeConverter(typeof(ExpandableObjectConverter))]
public class ExpandablePersonObject : FrameworkElement
{
    private string _Name = "";
    private int _Age = 0;
    private Brush _ShirtColor = Brushes.Blue;

    public string Name
    {
        get { return _Name; }
        set { _Name = value; }
    }

    public int Age
    {
        get { return _Age; }
        set { _Age = value; }
    }

    public Brush ShirtColor
    {
        get { return _ShirtColor; }
        set { _ShirtColor = value; }
    }
}

In Visual Studio 2010 the issue is with the attribute DesignTimeVisible(false): when I drag the dll file into the Toolbox it shows the PersonObject class too.

In Visual Studio 2013 the issue is with the attribute ToolboxBrowsable(false): when I drag the dll file into the Toolbox the PersonObject and ExpandablePersonObject classes are not showed (and this is ok) but if I go in the properties of my custom control and I try to press the New button near the ExpandablePersonObject property I get the error: "No types are available for you to add to this collection".

What am I doing wrong? How can I make my WPF custom control library compatible with both VS2010 and VS2013?

Thank you in advance

how to use radioButton(s) and image controls on windows phone 8.1

$
0
0
how to use radioButton(s) and image controls on windows phone 8.1

Binding to double => UpdateSourceTrigger=PropertyChanged.

$
0
0

<TextBox Text="{Binding eMin,UpdateSourceTrigger=PropertyChanged,StringFormat={}{0:0.##}}" Grid.Row="1" Grid.Column="1" Style="{DynamicResource ReadOnlyStyle}"/>

        public double eMin
        {
            get { return _eMin; }
            set { _eMin = value;
            NotifyPropertyChanged();
            }
        }

Basically, when I want to type for example, 2.5, I have to first type 25 and then put a dot in between 2 & 5. It's because I set UpdateSourceTrigger=PropertyChanged.

Now I got to implement UpdateSourceTrigger as discussed in here, for all textbox. 


ListView + MVVM: How do I get the value of a selected cell?

$
0
0

Hi,

I love programming WPF using MVVM, but sometimes easy looking problems lead to unbearable suffering.

Here is my problem: I have a simple list view with about a couple of columns. All I want to do is to copy the cell content to clipboard, when the user right-clicks on a cell.

<ListView Grid.Row="0" ItemsSource="{Binding Articles}" SelectedItem="{Binding SelectedArticle}" SelectionMode="Single"><ListView.View><GridView><GridView.Columns><GridViewColumn Header="ID" Width="70" DisplayMemberBinding="{Binding Path=ID, Mode=OneWay}" /><GridViewColumn Header="Name" Width="150" DisplayMemberBinding="{Binding Path=Name, Mode=OneWay}"/><GridViewColumn Header="Price" Width="100" DisplayMemberBinding="{Binding Path=Price, Mode=OneWay}"/><GridViewColumn Header="Description" Width="70" DisplayMemberBinding="{Binding Path=Description, Mode=OneWay}"/></GridView.Columns></GridView></ListView.View></ListView>

Without MVVM I probably would have used the some Click-Event and extract the info from the event args. But of course I don't want to break the MVVM pattern.

But if I use some kind of event binding I cannot find a way to retrieve event args or the sender object:

<i:EventTrigger EventName="PreviewMouseRightButtonDown"><i:InvokeCommandAction Command="{Binding CopyCellValueToClipCommand}"/></i:EventTrigger>

Using this trigger I can react to the desired event, but how can I retrieve the cell value?

I really hope you guys have a clue.

Thanks for your help,
Michael

How can I get the IsSelected property of the item in the tree view in this multivalue converter?

$
0
0

I Have this treeview:

<TreeView><TreeView.ItemContainerStyle><Style TargetType="{x:Type TreeViewItem}"><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type TreeViewItem}"><Grid><Grid.ColumnDefinitions><ColumnDefinition MinWidth="19" Width="Auto"/><ColumnDefinition Width="Auto"/><ColumnDefinition Width="*"/></Grid.ColumnDefinitions><Grid.RowDefinitions><RowDefinition Height="Auto"/><RowDefinition/></Grid.RowDefinitions><ToggleButton x:Name="Expander" ClickMode="Press" IsChecked="{Binding IsExpanded, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource ExpandCollapseToggleStyle}"/><Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Column="1" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true"><ContentPresenter x:Name="PART_Header" ContentSource="Header" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/></Border><ItemsPresenter x:Name="ItemsHost" Grid.ColumnSpan="2" Grid.Column="1" Grid.Row="1"/></Grid><ControlTemplate.Triggers><Trigger Property="IsExpanded" Value="false"><Setter Property="Visibility" TargetName="ItemsHost" Value="Collapsed"/></Trigger><Trigger Property="HasItems" Value="false"><Setter Property="Visibility" TargetName="Expander" Value="Hidden"/></Trigger><Trigger Property="IsEnabled" Value="false"><Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/></Trigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter><Setter Property="IsExpanded" Value="{Binding IsExpanded, NotifyOnSourceUpdated=True}" /><Setter Property="IsSelected" Value="{Binding IsSelected, NotifyOnSourceUpdated=True}" /><Setter Property="FontWeight" Value="Normal"/><Style.Triggers><Trigger Property="IsSelected" Value="True"><Setter Property="FontWeight" Value="Bold" /></Trigger></Style.Triggers><Style.Resources><SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/><SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/><SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Transparent"/><SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="Black"/><SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/></Style.Resources></Style></TreeView.ItemContainerStyle><TreeView.Resources><HierarchicalDataTemplate DataType="{x:Type ViewModels:MyNodeViewModel}" ItemsSource="{Binding Path=Hijos}"><StackPanel Orientation="Horizontal"><TextBlock Text="(DT)"><TextBlock.Style><Style TargetType="{x:Type TextBlock}"><Setter Property="Background"><Setter.Value><MultiBinding Converter="{StaticResource MyMultiValueConverter}" ConverterParameter="0"><MultiBinding.Bindings><Binding /><Binding RelativeSource="{RelativeSource Mode=Self}" Path="IsSelected"/></MultiBinding.Bindings></MultiBinding></Setter.Value></Setter></Style></TextBlock.Style></TextBlock></StackPanel></HierarchicalDataTemplate></TreeView.Resources></TreeView>

In  the multibinding, I am trying to use the IsSelected property in my multivalue converter, but I get DependencyProperty.UnsetValue, because the IsSelected property is not found.

How can I get the IsSelected property of the item in the treeview?

Thanks so much.

Only Storyboard.Completed event found - where are the other events?

$
0
0

I'm using Microsoft Visual Studio Ultimate 2013 with a project with Target Windows Phone OS Version set to Windows Phone 8.0. I have a System.Windows.Media.Animation.StoryBoard in my code, and via intellisense I see listed the Completed event. Going to the definition of its base class, System.Windows.Media.Animation.Timeline, I likewise see: public event EventHandler Completed. Looking at the documentation for Timeline (https://msdn.microsoft.com/en-us/library/system.windows.media.animation.timeline%28v=vs.110%29.aspx) I see there are 6 events for Timeline, namely: Changed, Completed, CurrentGlobalSpeedInvalidated, CurrentStateInvalidated, CurrentTimeInvalidated, and RemoveRequested. Why do I only see the Completed event in Visual Studio? I'm also wondering about the state of the documentation - it shows details for the Completed event, but the CurrentTimeInvalidated shows "no content" when I click on it.


How do I capture a ctrl-o key entry using KeyEventsArgs?

$
0
0

How do I capture a ctrl-o key entry KeyEventsArgs? ... private void Window_KeyDown(object sender, KeyEventArgs e)

Visual Studio does not like any of these:

    if (e.Control && e.Key == Key.O)
    if (e.Key == Control && e.Key == Key.O)
    if (e.Key == Key.Control && e.Key == Key.O)

bhs67

On selecting an item in combobox, UI goes behind the main application but combobox are still functional.

$
0
0

On some machines, we are facing an issue, where sometimes if we select an item in combobox, the main UI goes into background.

Suppose a User control has 2 combobox. Selecting an item in first shifts the UI into the background and when user clicks on the area, where second combobox was, before UI went into background, the dropdown list appears but still the User control remains in background.

This User control has been launched in a non-modal way( so that main app Excel 2010 can be accessed) and topmost property has been set to true, the owner of this UI has been set to excel using windowinterophelper so that it doesn't float on other application.

All our machines have Win 7, 64 bit.

This issue occurs sporadically but when it happens, it occurs on all UIs, for all combobox. Only way to use the application in this situation, is to restart the application. Any ideas or workarounds to avoid this behavior?

listbox scroll issue - loses data

$
0
0

hi,

I have a listbox control, I load it up with 20-30 images, and then enter some text into textboxes in the row template, I scroll down to the bottom and then scroll back to the top - the data I entered into the text boxes on the top rows is being removed!

there is no code associated with the scroll events, so I have no idea what is happening...

Any ideas?

thx

MrP

How to get the XML file path ?

$
0
0

In my code i want to open and read a xml file. in C# I did like this:

XmlTextReader textReader;
            textReader = new XmlTextReader(Path.GetDirectoryName(Path.GetDirectoryName(Application.StartupPath)) + "\\highS" + Level + ".xml");

            textReader.Read();

when I do the same thing in WPF I get an error:

Could not find file 'Z:\WPF\Kakuro WPF 17_02_2015\Kakuro\bin\Debug\highS2.xml'.

what should I do?



automatically update 2 dimensional array based on list content

$
0
0

Suppose I have a simple class named SimplePoint

class SimplePoint
{
	public int x { get; set; }
	public int y { get; set; }
};

Next I create an ObservableCollection of the class and bind it to a WPF DataGrid via XAML.

Each SimplePoint has a distance to each other SimplePoint int the list, resulting in a matrix. The distance from point a to b can be different from the distance from point b to a (ie. different route).Distances should be editable.

I wonder how I can create a class, that can automatically keep track of changes in the ObservableCollection<SimplePoint>. When a new SimplePoint is added, one row and one column should be added to the distances matrix. When a SimpePoint is removed, the appropriate entries in the matrix should be removed. Later I also wish to store and retrieve all the info to and from an XML via serialization.

So this list:

  • point1
  • point2
  • point3

Would yield a 3x3 matrix of distances.

Searching a lot did not yield results, though I am unsure if I looked for the right thing. I cannot get my head around how to organize this internally. Maybe something like ObservableCollection<ObservableCollection<SimplePoint>>, but then how to maintain that, and how to bind it to a DataGrid... Any ideas?

Converting TextReader.Value to Int

$
0
0

int Rpoint = 0; Rpoint = Convert.ToInt32(textReader.Value);

I have an error :

Input string was not in a correct format.

What should I do ?

How do I gracefully handle XamlParseExceptions?

$
0
0

If a control that is dynamically loaded as part of a template has an error, it causes a XamlParseException which crashes the entire application.

I can hook into the Application.DispatcherUnhandledException event, but setting Handled to true on the DispatcherUnhandledExceptionEventArgs argument does not disable the problematic control. As a result, WPF tries again to apply the template, causing the error to be thrown once more, resulting in an infinite loop.

Is there any way to disable the problematic control so that it stops trying to render?

Here is how to reproduce this scenario:

1. Create a WPF application.

2. Add a Window called "Dialog1" and a UserControl called "UserControl1".

3. Add the following code to each file (namespaces and root XAML elements left out):

App.xaml.cs:

(Also add the following namespace reference to the top: using System.Windows.Threading;)

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        DispatcherUnhandledException += OnDispatcherUnhandledException;
    }

    private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
    {
        e.Handled = true;
    }
}

Dialog1.xaml:

(Also add the following namespace reference: xmlns:local="clr-namespace:your app namespace")

<StackPanel><Button Content="Cause XamlParseException" Click="Button_Click" /><ListBox ItemsSource="{Binding Mode=OneWay}"><ListBox.ItemTemplate><DataTemplate><local:UserControl1 /></DataTemplate></ListBox.ItemTemplate></ListBox></StackPanel>

Dialog1.xaml.cs:

public partial class Dialog1 : Window
{
    public Dialog1()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var list = new object[1];
        list[0] = new object();

        DataContext = list;
    }
}

MainWindow.xaml:

<StackPanel><Button Content="Show Dialog" Click="Button_Click" /></StackPanel>

MainWindow.xaml.cs:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var dlg = new Dialog1();
        dlg.ShowDialog();
    }
}

UserControl1.xaml.cs:

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();

        throw new Exception();
    }
}

Run the solution, click the "Show Dialog" button, and then click the "Cause XamlParseException" button. This will cause the exception to be thrown repeatedly.

TwoWay binding doesn't work using StringFormat={}{#:#.####}, why and how to fix it?

$
0
0
<TextBox  Text="{Binding doResult,UpdateSourceTrigger=PropertyChanged,StringFormat={}{#:#.####},Mode=TwoWay}" Grid.Row="8" Grid.Column="1" Visibility="{Binding ODvisbility,Converter={StaticResource BooleanToVisibilityConverter}}" Style="{Binding ODStyle}"/>

After changing the stringFormat from 0:0.#### to #:#.#### twoWay binding has stop responding.  

I fixed one problem and end up with another!

Changing a Label Content of Page 2 from Page in WPF

$
0
0
Good Day All 
 
I have two WPF Pages Page 1 and Page 2. i want to change the a Label of Page 2 from Page 1. 
 
tried to create an Instance of Page 2 in Page 1 and access the label and Change the content property before i explore the Property Solution if there is anyone who has a different solution. please help
 
Thanks

Vuyiswa Maseko

Viewing all 18858 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>