WPF C# set trigger property in control by code behind [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
<ContentControl Width="130"
Height="130"
Canvas.Top="60"
Canvas.Left="50"
***Selector.IsSelected="True"***
Style="{StaticResource DesignerItemStyle}">
I would like to set property Selector.IsSelected to ContentControl by using code behind. But I don't know how to do it. Please help me and give me some example.

If you want to set an attached dependency property in code you do this
ContentControl x;
//To set the value
x.SetValue(Selector.IsSelectedProperty, true);
//To Clear the value
x.ClearValue(Selector.IsSelectedProperty);
//Set using the static function on Selector
Selector.SetIsSelected(x, true);

For accessing a control in Code-behind you need first provide it a name -
<ContentControl
x:Name=""ContentControl1"
Width="130"
Height="130"
Canvas.Top="60"
Canvas.Left="50"
***Selector.IsSelected="True"***
Style="{StaticResource DesignerItemStyle}">
and then you can access it in code and set the value as mentioned in other answer -
ContentControl1.SetValue(Selector.IsSelectedProperty, true);
Apart from this it would be a good idea to look at creating a property in code-behind or ViewModel(MVVM) and bind that directly to your control like this -
<ContentControl
Width="130"
Height="130"
Canvas.Top="60"
Canvas.Left="50"
Selector.IsSelected="{Binding IsSelectedBoolProperty, Mode=OneWay}"
Style="{StaticResource DesignerItemStyle}">
This techniques will be very useful in case you have a lot of controls in your window and I would suggest you to look at implementing MVVM in your application to avoind doing these kind of things in code-behind.

Related

How to rename an item in a ListBox? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I want the user to be able to directly rename an item in a ListBox with the same effect as we can see in the file explorer of Windows by example. Like this:
Is there a simple way to achieve this?
Thanks for your answers.
Assuming your item class has a read/write property that is displayed in the ListBox, e.g.
public class MyItem
{
public string ItemText { get; set; }
}
you could bind the Text property of a TextBox in the ItemTemplate of the ListBox to that property:
<ListBox x:Name="ListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding ItemText}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
In order to have the ItemText property updated while you type into the TextBox (in contrast to when the TextBox loses focus), you would have to set the Binding's UpdateSourceTrigger:
<TextBox Text="{Binding ItemText, UpdateSourceTrigger=PropertyChanged}"/>

WPF - Best code architecture for handling numerous tab items in Mainwindow [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I'm trying to create a WPF application where the homepage (aka MainWindow) will contain several tabs.
I'm wondering what would be the best approach/architecture to encapsulate code specific to each tab instead of coding everything in the same place ?
NOTE
After some research I found UserControl class, but I've always thought that class was use in order to avoid re-coding the same logic over and over. In my case I would be using them once for every tab.
Depending on your preference and desired level of complexity, you can use MVVM or just create a UserControl for every tab to better distribute the code. Resulting in your window's xaml looking something like:
<TabItem x:Name="tab_peerView" Header="Peers (Lobby)" Visibility="Visible">
<Views:SomeUserControl1 />
</TabItem>
<TabItem x:Name="tab_PrepCheckList" Header="Prep" Visibility="Visible">
<Views:SomeUserControl2 x:Name="view_prep" />
</TabItem>
Doing this method you can also offload things like command bindings and respective Execute and CanExecute to the UserControls.

How do I get data from a Text Block that is in a Data Template in a List View? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I made a list and populated it with the file urls, I want to grab that url from the text block that is in the list inside of that data template but the function that I made for that purpose isn't returning me expected string. It is giving me null instead. The function I used was SelectionChanged property of the ListView
<ListView ItemsSources="{x:Bind noticeData}"
SelectedIndex="{x:Bind MasterListView.SelectedIndex, Mode=OneWay}"
x:Name="uriList" SelectionChanged="uriList_SelectionChanged"
IsItemClickEnabled="True" Grid.Row="3">
<ListView.ItemTemplate>
<DataTemplate x:DataType="data:Datum_2">
<StackPanel>
<TextBlock x:Name="downFileUri" Grid.Row="2" Grid.Column="0"
VerticalAlignment="Center" HorizontalAlignment="Center"
Margin="0,0,0,20" Visibility="Visible"
Text="{x:Bind file_url}" TextWrapping="Wrap"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
When following the MVVM pattern you will more often than not, skip using control event handlers, such as click events and selection events. Instead, the pattern allows us to bind to properties and capture when those property's change inside the properties setting block.
Instead of binding to the SelectedIndex you want to bind to the SelectedItem
The SelectedItem will be of whatever type your collection is holding. In the below example I assume you have an object called NoticeData
<ListBox SelectedItem="{Binding MySelectedItem, Mode=TwoWay}">
Then in your view model
private NoticeData _mySelectedItem;
public NoticeData MySelectedItem
{
get{ return _mySelectedItem; }
set{ _mySelectedItem = value;}
}
Here is the MSDN documentation on ListBox as you will see there is both SelectedItem and SelectedItems properties. These can both be bound to. SelectedItems of course is used if you enable multiple selection.
Something else you will likely need to consider is implementing INotifyPropertyChanged on your ViewModel if you haven't already.
Here is the MSDN documentation for that as well
Edit based on comments
There is an alternative method that you can use for binding as well.
If you wish to bind another element to your selected item you just need to name your element.
<ListBox x:Name="MyListBox" ItemsSource="{Binding myItems}"/>
<TextBlock Text="{Binding ElementName=MyListBox, Path=SelectedItem.WhatEverProperty}"/>

WPF - Make a window re-open when minimized [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I've been trying to make a program, that makes an ellipse move on the screen (by the arrow keys), when I am still able to click the window that is open behind it (for example - Google Chrome) and the ellipse will still be visible and move-able.
I've been trying a lot of things (including TopMost) and nothing worked.
Now, my idea is to make a transparent window, that is un-clickable and will re-open (will be maximized) every time it gets minimized.
Can someone please help me? I have no code that can help, the names of the objects involved don't matter.
I think your questions are already answered elsewhere:
Click-through control in WPF (use <TextBlock IsHitTestVisible="False" .../>)
Preveting the window from minimizing:
a. Cancel minimizing event (intercept the minimize event and cancel it)
b. Preventing from minimizing on "Show Desktop" (Win+D) command (mark the window always-on-top)
I am not sure if I understood your question right, you want to click through the ellipse.
You could for example just use two grids with different ZIndex and just Register when 1 gets clicked. Eg:
<Grid>
<Grid Background="Transparent" Panel.ZIndex="1">
</Grid>
<Grid Panel.ZIndex="0">
<Ellipse Background="Blue" Height="100" Width="200"/>
</Grid>
</Grid>

Creating Photo Gallery with Silverlight in Windows Phone 7.5 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm looking to create an app that features a photo gallery, one that is almost exactly the same as the built in photo gallery/photos hub.
I've looked at using a Grid, but I can't bind data to it. I also looked at using a DataGrid, but that is not included in the Windows Phone SDK. The grid I need will have a set number of columns but a variable number of rows (like I said, just like the current built in photo gallery)
Does anyone have any suggestions/solutions?
Photo gallery is simply a listbox where every row is an horizontal stackpanel with images, add a little binding and you are ok ;)
It will work with a listbox, but you have to edit its ItemsPanelTemplate and use something like the WrapPanel control (horizontal orientation) from the toolkit. Then, you can define the ItemTemplate as a square image. That way every new item will stack at the right from the other until there's no more room and it will continue on the next row. So for a set number of columns, you have to specify the width in the item template (173 in my example so in the portrait mode, i end with 2 columns).
Here's a example from one of my projects (you should adjust bindings and names to your scenario):
<ListBox x:Name="lbxCategorias" ItemsSource="{Binding ChannelButtons}"
SelectionChanged="lbxCategorias_SelectionChanged">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Image Source="{Binding BigButtonIconPath}" Width="173" Height="173" Margin="0 0 12 10" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The Windows Phone SDK contains a Panorama/Pivot Control that mimics the functionality you see in the people or photos hub.
Here's a link to a code sample using this control:
http://msdn.microsoft.com/en-us/library/ff431744(v=vs.92).aspx
Scroll down to the Controls section of the samples page and the first item there is the Panorama Sample.
I'd also recommend taking a look at the section titled Cameras and Photos.
You can build a exact photo viewer like in wp7 using the horizontal looping selector with some modifications. Try to add the images in the item i think it helps you
http://blog.supaywasi.com/2011/06/horizontal-looping-selector/

Categories

Resources