I have an image that is inside of a tab item:
<TabItem x:Name="tabThreeTb" Header="Photos" HorizontalAlignment="Left" Height="22" VerticalAlignment="Top" Width="55" Margin="1,0,-1,0">
<Grid x:Name="tabThreeBdy" Background="#FFE5E5E5">
<Rectangle Fill="#FFE5E5E5" HorizontalAlignment="Left" Height="369" Margin="12,13,0,0" Stroke="Black" VerticalAlignment="Top" Width="467">
<Rectangle.Effect>
<DropShadowEffect/>
</Rectangle.Effect>
</Rectangle>
<TextBox x:Name="picNotesTextBox" HorizontalAlignment="Left" Height="415" Margin="498,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="299"/>
<Button x:Name="nxtPhotoBtn" Content="Next" HorizontalAlignment="Left" Margin="404,403,0,0" VerticalAlignment="Top" Width="75"/>
<Button x:Name="prevPhotoBtn" Content="Prev" HorizontalAlignment="Left" Margin="10,403,0,0" VerticalAlignment="Top" Width="75"/>
<Label x:Name="photoNumLbl" Content="1 of 4" HorizontalAlignment="Left" Margin="226,401,0,0" VerticalAlignment="Top" Width="42"/>
<Image x:Name="photoTabImage" HorizontalAlignment="Left" Height="369" Margin="12,13,0,0" VerticalAlignment="Top" Width="467" AllowDrop="True" DragEnter="photoTabImage_DragEnter"/>
</Grid>
</TabItem>
I am trying to use drag and drop to allow for the addition of photos to to the list that contains paths for the image source, though I can't seem to be able to fire the DragEnter routine...
I would like the drag and drop functionality to only be alive when the content is being dragged over the Image bounds.
Is there something I need to do for an Item that is nested in a tab control to allow this?
The issue is in that your app for some reason can't proceed standard events set. To fix that switch it to the tunneling model, by simply replacing your events on Preview versions (for instance replace DragEnter="photoTabImage_DragEnter" on to the PreviewDragEnter="photoTabImage_DragEnter")
Best regards,
Maks!
try add
<Label HorizontalAlignment="{Binding ElementName=photoTabImage, Path=HorizontalAlignment}"
Height="{Binding ElementName=photoTabImage, Path=Height}"
Width="{Binding ElementName=photoTabImage, Path=Width}"
Margin="{Binding ElementName=photoTabImage, Path=Margin}"
VerticalAlignment="{Binding ElementName=photoTabImage, Path=VerticalAlignment}"
AllowDrop="True" Drop="ContainerDrop" DragOver="ContainerDragOver"/>
after Image, and use event DragOver
if you need analyze drop object then add next code inside you class
private void ContainerDrop(object sender, DragEventArgs e)
{
StringBuilder sb = new StringBuilder();
foreach (string format in e.Data.GetFormats())
{
sb.AppendLine("Format:" + format);
try
{
object data = e.Data.GetData(format);
sb.AppendLine("Type:" + (data == null ? "[null]" : data.GetType().ToString()));
sb.AppendLine("Data:" + data.ToString());
}
catch (Exception ex)
{
sb.AppendLine("!!CRASH!! " + ex.Message);
}
sb.AppendLine("=====================================================");
}
Console.WriteLine(sb.ToString());
}
private void ContainerDragOver(object sender, DragEventArgs e)
{
e.Effects = DragDropEffects.Copy;
e.Handled = true;
}
Related
i want to send Data from one Textbox on window One to a label on window Two.
starting with window two:
<StackPanel>
<StackPanel x:Name="ButtonStackPanel" Height="Auto" Width="Auto">
<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Vertical">
<Button Style="{DynamicResource ButtonStyle}" Content="To The Dark Side" Click="OnClickToDarkSide"/>
<Button Style="{DynamicResource ButtonStyle}" Content="To The Gray" Click="OnClickToGraySide"/>
<Button Style="{DynamicResource ButtonStyle}" Content="To The Light Side" Click="OnClickToLightSide"/>
</StackPanel>
<Border HorizontalAlignment="Center" VerticalAlignment="Stretch" Background="Red" Height="Auto" Width="2"/>
<Label Style="{DynamicResource LabelStyle}" x:Name="theTextBlock" Content="{Binding Source=CodeText}"/>
<Border HorizontalAlignment="Center" VerticalAlignment="Stretch" Background="Red" Height="Auto" Width="2"/>
<ToggleButton Style="{DynamicResource ToggleButtonStyle}" Content="Open Style Window" Name="StyleWindowButton" Click="OnClickOpenStyleWindow"/>
<ToggleButton Style="{DynamicResource ToggleButtonStyle}" Content="Open Text Window" Name="TextWindowButton" Click="OnClickOpenTextWindow"/>
</StackPanel>
<Border Height="2" Width="Auto" Background="Red" HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
</StackPanel>
<Border Height="2" Width="Auto" Background="Red" HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
</StackPanel>
Codebehind of Window Two:
public MainWindow()
{
(App.Current as App).CodeText = _jediCode;
InitializeComponent();
}
private void OnClickToDarkSide(object sender, RoutedEventArgs e)
{
(App.Current as App).ChangeSkin(Skin.Dark);
(App.Current as App).CodeText = _sithCode;
theTextBlock.Content = (App.Current as App).CodeText;
}
private void OnClickToLightSide(object sender, RoutedEventArgs e)
{
(App.Current as App).ChangeSkin(Skin.Light);
(App.Current as App).CodeText = _jediCode;
theTextBlock.Content = (App.Current as App).CodeText;
}
private void OnClickToGraySide(object sender, RoutedEventArgs e)
{
(App.Current as App).ChangeSkin(Skin.Gray);
(App.Current as App).CodeText = _grayCode;
theTextBlock.Content = (App.Current as App).CodeText;
}
private void OnClickOpenStyleWindow(object sender, RoutedEventArgs e)
{
if (StyleWindowButton.IsChecked == true)
{
styleWindow = new StyleWindow();
styleWindow.Show();
}
else
{
styleWindow.Close();
styleWindow = null;
}
}
private void OnClickOpenTextWindow(object sender, RoutedEventArgs e)
{
if (TextWindowButton.IsChecked == true)
{
textWindow = new InputWindow();
textWindow.Show();
}
else
{
textWindow.Close();
textWindow = null;
}
}
}
window one:
<Grid>
<TextBox HorizontalAlignment="Left" VerticalAlignment="Top" Height="200" Width="200" TextWrapping="Wrap"
AcceptsReturn="True" AcceptsTab="True" Text="{Binding Path=CodeText, Source={x:Static Application.Current}, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Margin="10,10,0,0">
<!-- TODO: trigger change from this textbox to the textblock in mainwindow -->
</TextBox>
</Grid>
code behind of one is empty.
app.xaml.cs:
public string CodeText
{
get => _codeText;
set { _codeText = value; OnPropertyChanged(nameof(CodeText)); }
}
Ok, the current behavior is clicking on one of the buttons (Dark Side, Gray, Light Side) leads to changes in the CodeText Property, which leads to a change of the content of the label of Window Two and the text of TextBox of Window One. Changing the text of the TextBox, changes also the CodeText Property, but does not lead to a change in the label and thats confusing, why does it work the one way, but not the other.
hope you have a hint for me. :) Maybe i missed a trigger or a kind of refresh for the label
bindings in window One and Two are set differently. (window Two does it wrong, Content="{Binding Source=CodeText}" is not valid binding)
in fact, window Two removes the binding by assigning CodeText directly as local value:
theTextBlock.Content = (App.Current as App).CodeText;
you should remove that line, and use the same binding as in window One:
<Label Style="{DynamicResource LabelStyle}" x:Name="theTextBlock"
Content="{Binding Path=CodeText, Source={x:Static Application.Current}}"/>
I have 2 button but they need to be in a different file than the mainWindow.cs.
I can't figure how to do that.
So the Button_Click_2 must be in the ReadData.cs and the
Button_Click_3 must be in the WriteData.cs
The app don't recognize the button when they are not in the mainWindow.
How can I do that ?
ReadData.cs :
public new void Button_Click_2(object sender, RoutedEventArgs e)
{
string text = verifyCard("5"); // 5 - is the block we are reading
textBlock1.Text = text.ToString();
}
MainWindow.xaml.cs :
public void Button_Click_2(object sender, RoutedEventArgs e)
{
//When I click it don't detected the code in the ReadData.cs
// The code of the button must be in the ReadData.cs
}
MainWindow.xaml :
<Grid>
<Button Content="Connexion" HorizontalAlignment="Left"
Margin="265,142,0,0" VerticalAlignment="Top" Width="236" Height="44"
Click="Button_Click_1"/>
<Button Content="Lire donnée carte" HorizontalAlignment="Left"
Margin="265,276,0,0" VerticalAlignment="Top" Width="236" Height="42"
Click="Button_Click_2"/>
<Button Content="Ecrire donnée carte" HorizontalAlignment="Left"
Margin="265,344,0,0" VerticalAlignment="Top" Width="236" Height="41"
Click="Button_Click_3"/>
<TextBox Name="textBox1" HorizontalAlignment="Left" Height="23"
Margin="321,224,0,0" TextWrapping="Wrap" Text="TextBox"
VerticalAlignment="Top" Width="120" TextChanged="TextBox_TextChanged"/>
<TextBlock Name="textBlock1" HorizontalAlignment="Left"
Margin="291,95,0,0" TextWrapping="Wrap" VerticalAlignment="Top"
Height="23" Width="177"/>
</Grid>
It must be possible to forced the app to detected and execute the code in another file than the Main.
I'm stuck... do any of you have the solution ?
You can't just place the button code in ReadData.cs class. as the event is related to ui of MainWindow.xaml create an object for ReadData do something like this
public new void Button_Click_2(object sender, RoutedEventArgs e)
{
ReadData rd= new ReadData();
string text = rd.verifyCard("5");
textBlock1.Text = text.ToString();
}
I have a gridView.
ıt has an Itemptemplate (textblock, image) and itemsource. textBlock and Image are binded to itemsource.
I want to add a button to ItempTemplate but I couldn't able to detect the eventHandler.
In my .cs file I dont see textblock, image or button.
how can I set the event,
here is the code of item template
<DataTemplate x:Key="IDViewStyle">
<Grid Width="350" Height="450" >
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<Grid>
<Border Background="#B2060606" />
<Button HorizontalAlignment="Right" BorderThickness="0" x:Name="eraseButton" VerticalAlignment="Top">
<Image Source="/Assets/Images/erease.png" Width="90" Margin="0,-7,-15,15"/>
</Button>
<StackPanel Margin="0" Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock HorizontalAlignment="Center" TextWrapping="Wrap" Text="+" VerticalAlignment="Center" Style="{StaticResource PageHeaderTextStyle}" FontSize="160" Margin="0"/>
<TextBlock TextWrapping="Wrap" Text="Ekle" TextAlignment="Center" Style="{StaticResource HeaderTextStyle}"/>
</StackPanel>
<Image Stretch="Fill" Source="{Binding Image}"/>
</Grid>
<TextBlock TextWrapping="Wrap" Text="{Binding Type}" VerticalAlignment="Top" Grid.Row="1" Style="{StaticResource SubheaderTextStyle}" TextAlignment="Center"/>
</Grid>
</DataTemplate>
and my .cs file
Data.IdentityTypeCollection collection;
gView.SelectionChanged += lvIdTypes_SelectionChanged;
collection = new Data.IdentityTypeCollection();
gView.ItemsSource = collection;
gView.ScrollIntoView(collection);
and my mainpage.xaml
<GridView x:Name="gView" Grid.Row="1" Grid.RowSpan="2" Margin="117,0,0,100" ItemTemplate="{StaticResource IDViewStyle}"/>
how can I use button event in the item template
On GridView you can assign a property ItemClick="YourClickEvent" like:
<GridView
x:Name="itemGridView"
IsItemClickEnabled="True"
ItemClick="ItemView_ItemClick"></GridView>
and in your .cs file the event handler:
void YourClickEvent(object sender, ItemClickEventArgs e)
{
//your codes here
}
Make sure GridView IsItemClickEnabled is set to True All set!
If I understand your question then this is what I did after using the debugger to look around. This is a UWP application. The Button is in a Grid in a DataTemplate in a "MedView" GridView. The following sets the ItemsSource:
List<ViewItemClass> lvil = new List<ViewItemClass>();
// create lvil
MedView.ItemsSource = lvil;
Then I added a Click event handler for the Button and the following is the code for that:
Button b = sender as Button;
if (b == null)
return;
Grid g = b.Parent as Grid;
if (g == null)
return;
ViewItemClass lvi = g.DataContext as ViewItemClass;
if (lvi == null)
return;
// use the item
In my wpf application, there is View class where I've ListBox. I wrote the code for double click event of ListBox Item.so when I double click on any list Box Item that item will be posted in my Harvest account.Here is the event:
private void listBox1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
//Submit clicked Entry
try
{
ListBoxItem item = (ListBoxItem)sender;
Harvest_TimeSheetEntry entryToPost = (Harvest_TimeSheetEntry)item.DataContext;
if (!entryToPost.isSynced)
{
//Check if something is selected in selectedProjectItem For that item
if (entryToPost.ProjectNameBinding == "Select Project" && entryToPost.ClientNameBinding == "Select Client")
MessageBox.Show("Please select you Project and Client");
else
Globals._globalController.harvestManager.postHarvestEntry(entryToPost);
MessageBox.Show("Entry posted");
}
else
{
//Already synced.. Make a noise or something
MessageBox.Show("Already Synced;TODO Play a Sound Instead");
}
}
catch (Exception)
{ }
}
My xaml code:
<DataTemplate x:Key="DefaultDataTemplate">
<StackPanel Orientation="Horizontal" Width="596">
<TextBox Text="{Binding ClientNameBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="145"/>
<TextBox Text="{Binding ApplicationNameBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="90"/>
<TextBox Text="{Binding StartTimeBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="100"/>
<TextBox Text="{Binding StopTimeBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="60"/>
<TextBox Text="{Binding ProjectNameBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="130"/>
<TextBox Text="{Binding TaskNameBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="71"/>
</StackPanel>
</DataTemplate>
<!-- Editable DataTemplate -->
<DataTemplate x:Key="EditableDataTemplate">
<StackPanel Orientation="Horizontal" Width="596">
<ComboBox x:Name="ClientComboBox" SelectionChanged="ProjectComboBoxChanged" ItemsSource="{Binding Path=clientList, ElementName=MainWin}" SelectedValuePath="_id" DisplayMemberPath="_name" SelectedItem="{Binding ClientNameBindingClass, Mode=OneWayToSource}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" Width="145"/>
<TextBox Text="{Binding ApplicationNameBinding}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="90"/>
<TextBox Text="{Binding StartTimeBinding}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="100"/>
<TextBox Text="{Binding StopTimeBinding}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="60"/>
<TextBox Text="{Binding TaskNameBinding}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="130"/>
<ComboBox x:Name="ProjectComboBox" SelectionChanged="ProjectComboBoxChanged" ItemsSource="{Binding Path=projectList, ElementName=MainWin}" SelectedValuePath="_id" DisplayMemberPath="_name" SelectedItem="{Binding ProjectNameBindingClass, Mode=OneWayToSource}" Width="71" Background="Yellow" BorderThickness="0"/>
</StackPanel>
</DataTemplate>
<!-- DataTemplate Selector -->
<l:DayViewListDataTemplateSelector x:Key="templateSelector"
DefaultDataTemplate="{StaticResource DefaultDataTemplate}"
EditableDataTemplate="{StaticResource EditableDataTemplate}"/>
I've timer in my class which generates that EditableDataTemplate with two comboBoxes. My problem is, when I select Client and Project in ComboBoxes and double click on the entry, it's posted in my account but at that time I want it to convert from editableDataTemplate to DefaultDataTemplate (i.e those two comboboxes should become textboxes likewise in DefaultDataTemplate). How should I achieve this result?
I don't think the DataTemplateSelector offers a method of changing the data template on request, it is merely used to choose different templates for different types of data (not data states).
I think probably the best way would be to add a property, lets call it IsInEditMode, to your data model. You could then add both the TextBlock and the Combobox to your data template and toggle their visibility according to the value of IsInEditMode.
By the way: if you use the ListBox.SelectedItem property in your DoubleClick-eventhandler you can directly access the data model element without having to first get the ListBoxItem and then access
its data context.
private void listBox1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
//Submit clicked Entry
try
{
if(listBox1.SelectedItem is Harvest_TimeSheetEntry)
{
Harvest_TimeSheetEntry entryToPost = (Harvest_TimeSheetEntry)listBox1.SelectedItem;
if (!entryToPost.isSynced)
{
//Check if something is selected in selectedProjectItem For that item
if (entryToPost.ProjectNameBinding == "Select Project" && entryToPost.ClientNameBinding == "Select Client")
MessageBox.Show("Please select you Project and Client");
else
Globals._globalController.harvestManager.postHarvestEntry(entryToPost);
MessageBox.Show("Entry posted");
entryToPost.IsInEditMode = true; //set edit mode!
}
}
else
{
//Already synced.. Make a noise or something
MessageBox.Show("Already Synced;TODO Play a Sound Instead");
}
}
catch (Exception)
{ }
}
I'm a relative beginner with WPF so please bear with me. I have a simple app that converts farenheit values to celcius and vice versa. I thought I would have a play with refactoring this to MVVM so I moved everything from my code-behind to a separate class and then set the dataContext programmatically. However I'm getting a lot of ..'does not exist in context errors'. Where am I going wrong? Thanks
XAML
<Window x:Class="FarenheitCelciusConverter.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Temperature Converter" Height="500" Width="500"
xmlns:local="clr-namespace:FarenheitCelciusConverter">
<Grid HorizontalAlignment="Left" VerticalAlignment="Top" Height="473" Width="488">
<Label Height="28" HorizontalAlignment="Left" Margin="10,10,0,0" Name="lblF" VerticalAlignment="Top" Width="64" FontWeight="Bold">Farenheit</Label>
<Label Height="28" HorizontalAlignment="Left" Margin="10,42,0,0" Name="lblC" VerticalAlignment="Top" Width="64" FontWeight="Bold">Celcius</Label>
<TextBox Height="23" Margin="94,10,112,0" Name="tbFaren" VerticalAlignment="Top" Width="72" HorizontalAlignment="Left" />
<TextBox Height="23" Margin="94,42,112,0" Name="tbCelcius" VerticalAlignment="Top" Width="72" HorizontalAlignment="Left" />
<Button Margin="94,76,109,0" Name="btnConvert" Click="btnConvert_Click" Height="23" VerticalAlignment="Top" HorizontalContentAlignment="Center" Width="72" HorizontalAlignment="Left">Convert</Button>
<Image Name="image1" Stretch="Fill" Margin="94,112,240,228">
<Image.Source>
<BitmapImage DecodePixelWidth="200" UriSource="C:\Users\Winston\Pictures\thermometer.jpg"/>
</Image.Source>
</Image>
<TextBlock FontWeight="Bold" Height="21" Margin="195,12,173,0" Name="tblCelci" VerticalAlignment="Top" /><TextBlock FontWeight="Bold" Height="21" Margin="195,44,0,0" Name="tblFarenh" VerticalAlignment="Top" HorizontalAlignment="Left" Width="120" /><TextBlock FontWeight="Bold" Height="21" Margin="195,78,15,0" Name="tblCex" VerticalAlignment="Top" Foreground="Red" />
</Grid>
</Window>
Code behind
namespace FarenheitCelciusConverter
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
DataContext = new ConverterViewModel();
}
}
}
View Model
namespace FarenheitCelciusConverter
{
public class ConverterViewModel
{
private void btnConvert_Click(object sender, RoutedEventArgs e)
{
tblCex.Text = "";
try
{
if (tbCelcius.Text.Length != 0)
{
double celcius = Double.Parse(tbCelcius.Text);
if (celcius < 99999.0 && celcius > -99999.0)
{
tblFarenh.Text = Math.Round(1.8 * celcius + 32.0) + " F";
}
else
{
throw new OverflowException("Number limit exceeded!");
}
}
if (tbFaren.Text.Length != 0)
{
double farenh = Double.Parse(tbFaren.Text);
if (farenh < 99999.0 && farenh > -99999.0)
{
tblCelci.Text = Math.Round(0.555 * (farenh - 32.0)) + " C";
}
else
{
throw new OverflowException("Number limit exceeded!");
}
}
}
catch (Exception ex)
{
tblCex.Text = ex.Message;
}
}
}
}
When using MVVM data is passed back and forth from the View (Window1) and the ViewModel through Databinding. So each of your textboxes should be databound to a public property in your Viewmodel:
<TextBox Height="23" Margin="94,10,112,0" Name="tbFaren" VerticalAlignment="Top" Width="72" HorizontalAlignment="Left" Text="{Binding FahrenText}"/>
And your ViewModel will take the values in the properties, do something with them, and set the properties bound to the appropriate textboxes.
In this way, the Viewmodel is performing the logic and the View is interpreting the output according to the rules that you are giving it. Later on, you can change the rules in the View without messing with the ViewModel whereas using the codebehind often you have to explicitly set View settings alongside the program logic.
Also, be sure to implement iNotifyPropertyChanged on your ViewModel , otherwise the UI wont know when the databound property values have changed and it wont update. Check out this post for an example.
Also here is the MSDN article on Databinding in WPF.