How can I prevent my Window from showing up? - c#

I am writing an app, where users have to be in the same domain or have to log in.
The Codebehind for the StartupUri is below.
The Startup Uri is a TaskbarIcon imported from "http://www.hardcodet.net/taskbar" that should be closed in my oppinion but shows up.
So when the user is logged in there are multible TrayBarIcons. One of them without the bound information and one regular.
My Code in the View looks like this:
public TaskbarView()
{
windowService = new WindowService();
timebutlerAPIService = new TimebutlerAPIService();
mainViewModel = new MainViewModel();
anmeldenViewmodel = new AnmeldenViewModel();
if (anmeldenViewmodel.AnmeldungModel.User.Domain != "AAA")
{
this.Close();
windowService.OpenAnmeldenView(anmeldenViewmodel);
return;
}
controller = new TimebutlerController(timebutlerAPIService, windowService, mainViewModel, anmeldenViewmodel);
this.DataContext = mainViewModel;
mainViewModel.CloseAction = new Action(this.Close);
InitializeComponent();
}
The Icon still shows up and I cannot figure out why.
The XAML For my TaskbarView looks like that: (with xmlns:tb="http://www.hardcodet.net/taskbar")
<tb:TaskbarIcon IconSource="pack://application:,,,/Icons/Timebutler.ico"
ToolTipText="Stempeluhr">
<tb:TaskbarIcon.TrayPopup>
<Border Background="#1e264a"
BorderBrush="#cf2e2e"
BorderThickness="2"
Width="200"
Height="140">
<Grid Margin="5">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Label Content="{Binding AktuellerStatus.StatusBeschreibung}"
Foreground="White"
HorizontalAlignment="Center"
Grid.Row="0"
FontSize="14" />
<TextBlock Text="{Binding AktuelleWorktime}"
Foreground="White"
FontSize="13"
TextAlignment="Center"
Grid.Row="1" />
<Button Content="Start"
Command="{Binding EinstempelnCommand}"
Margin="12"
Background="#cf2e2e"
Foreground="White"
Grid.Row="2"
Visibility="{Binding IsPaused, Converter={StaticResource BoolToVis}}" />
<Button Content="Pause"
Command="{Binding PausierenCommand}"
Margin="12"
Background="#cf2e2e"
Foreground="White"
Grid.Row="2"
Visibility="{Binding IsRunning, Converter={StaticResource BoolToVis}}"/>
</Grid>
</Border>
</tb:TaskbarIcon.TrayPopup>
<tb:TaskbarIcon.ContextMenu>
<ContextMenu>
<MenuItem Header="Fenster öffnen"
Command="{Binding OpenOptionsViewCommand}">
<MenuItem.Icon>
<Image Source="pack://application:,,,/Icons/Timebutler.ico" />
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="Programm beenden"
Command="{Binding CloseCommand}">
<MenuItem.Icon>
<Image Source="pack://application:,,,/Icons/X.ico" />
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
</tb:TaskbarIcon.ContextMenu>
</tb:TaskbarIcon>
Changing the visibility to hidden hides the traybaricon I'd like it not to show up.

Related

Getting selected item in ListView without selecting an Item on UI

I'm working on a WPF application where I've made a ListView on UI which is two-way binding enabled. This WPF application is being developed for a windows 10 based smartphone. So whenever a user taps an item in the list whose data source is data-binded to back data collection, I've to do some operation for that item in code behind.
Now the problem is, I always receive a null object from the below function whenever the user taps on the item.
CartItem selectedItem = (CartItem)lv_CartItems.SelectedItem;
I only get a filled item object when the user actually selects an item by clicking on ListViewItem rather than tapping on it.
I want to get the selected item when the user taps on it. Is there any workaround available in WPF this problem? I've stuck here
My ListViewItem template looks like this.
<ListView Name="lv_CartItems" Loaded="lv_CartItems_Loaded"
HorizontalAlignment="Center" ScrollViewer.CanContentScroll="False"
ScrollViewer.VerticalScrollBarVisibility="Visible"
Width="250" Height="230" SelectionMode="Single"
>
<ListView.ItemTemplate>
<DataTemplate>
<Viewbox>
<Grid Width="230" Height="110" >
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width=".1*" />
<ColumnDefinition />
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition Width=".5*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="2*" />
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Border BorderBrush="LightGray" BorderThickness="1"
Grid.Row="0" Grid.Column="0"
Grid.ColumnSpan="6" Grid.RowSpan="3" >
</Border>
<Viewbox Grid.Row="0" >
<Image Name="img_ItemImage"
Source="{Binding Image, Mode=TwoWay }"
Width="20" Height=" 25" />
</Viewbox>
<Viewbox Grid.Column="2" Grid.ColumnSpan="3" VerticalAlignment="Top" >
<TextBlock Name="lbl_ItemName" TextWrapping="Wrap" Width="180" Foreground="Gray"
Text="{Binding Name , Mode=TwoWay }" >
</TextBlock>
</Viewbox>
<Viewbox Grid.Row="1" Margin="10,0" VerticalAlignment="Top" >
<TextBlock Foreground="Gray" >Qty:</TextBlock>
</Viewbox>
<Viewbox Grid.Row="2" Margin="0,0" VerticalAlignment="Top" >
<StackPanel Orientation="Horizontal" >
<Button Name="btn_Minus" FontWeight="ExtraBold" Tag="{Binding SKU_No,Mode=TwoWay}" Padding="0" Width="12"
Resources="{StaticResource cartitembutton}" Click="btn_Minus_Click" >
<Image Source="/Resources\Icons\minus.png" ></Image>
</Button>
<Border BorderThickness="1" Margin="2,0" Width="13" CornerRadius="2" BorderBrush="LightGray" >
<TextBlock Name="lbl_Quantity" FontWeight="Bold" Foreground="Gray"
HorizontalAlignment="Center" VerticalAlignment="Center"
Text="{Binding Quantity , Mode=TwoWay }" >
</TextBlock>
</Border>
<Button Name="btn_Increment" FontWeight="ExtraBold" Width="12"
Resources="{StaticResource cartitembutton}" Tag="{Binding SKU_No,Mode=TwoWay}"
Padding="0"
Click="btn_Increment_Click">
<Image Source="/Resources\Icons\union_10.png" ></Image>
</Button>
</StackPanel>
</Viewbox>
<Viewbox Grid.Row="1" Grid.Column="2" Margin="5,0"
HorizontalAlignment="Left" Grid.ColumnSpan="3" >
<TextBlock Name="lbl_Price" FontWeight="DemiBold"
Text="{Binding Price , Mode=TwoWay}" ></TextBlock>
</Viewbox>
<Viewbox Grid.Row="2" Grid.Column="2" Grid.ColumnSpan="3"
VerticalAlignment="Top" Margin="0,0" >
<TextBlock Name="lbl_Appearence"
Text="{Binding Appearance , Mode=TwoWay }"
TextWrapping="Wrap" Foreground="Gray" Width="210" >
</TextBlock>
</Viewbox>
<Viewbox Grid.Column="5" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="2,2"
>
<Button Name="btn_DeleteItem"
Click="btn_DeleteItem_Click"
Resources="{StaticResource cartitembutton}" >
<Image Source="/Resources/Icons/delete.png" ></Image>
</Button>
</Viewbox>
</Grid>
</Viewbox>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
This is how a listVeiw looks like.
You don't actually want the selected item, you want to know which item contained the button that was clicked- whether that item is selected or not.
You can do this with events, but the more "correct" way to do it in WPF is with commands.
Commands are a pattern which separate the logic of an action (e.g. deleting an item) from the visual component that triggers the action (e.g. the button the user clicks). A command includes a parameter, which you can use to specify which item the command is being executed on. There are a couple different types on commands in WPF, but I'll be explaining using RoutedCommand.
In your case you'd first declare your command(s) in your Window:
public static readonly RoutedCommand DeleteItemCommand =
new RoutedCommand("DeleteItem", typeof(MainWindow));
private void OnCanExecuteDeleteItem(object sender , CanExecuteRoutedEventArgs e)
{
e.CanExecute = e.Parameter != null;
}
private void OnExecuteDeleteItem(object sender, ExecutedRoutedEventArgs e)
{
SomeType item = (SomeType)e.Parameter;
//Actually delete the item
}
And you'd add a CommandBinding to your Window which links the command to the methods which handle it:
<Window.CommandBindings>
<CommandBinding Command="local:MainWindow.DeleteItemCommand" CanExecute="OnCanExecuteDeleteItem" Executed="OnExecuteDeleteItem"/>
</Window.CommandBindings>
And finally, to link your delete Button to the command:
<Button Name="btn_DeleteItem" Resources="{StaticResource cartitembutton}" Command="local:MainWindow.DeleteItemCommand" CommandParameter="{Binding}">
<Image Source="/Resources/Icons/delete.png"/>
</Button>
CommandParameter="{Binding}" works because your Button is inside a DataTemplate, so its DataContext will be the item that is being displayed. This lets you check e.Parameter in your routed event methods to see which item is being interacted with.

Accessing .xaml items from different .xaml.cs file?

I have .xaml items accross three files. In one .xaml I have option to change language at running.
Is there any chance how can I access items from others .xaml to change the language as well?
Changing language of .xaml items in MainWindow.xaml.cs
public partial class MainWindow : Window
{
string strLanguage = "";
Boolean boolInit = true;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Set_Language();
}
private void ddlLanguage_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Set_Language();
}
private void Set_Language()
{
if (boolInit == false)
{
strLanguage = "SMS_Vrana.Languages." + ((ComboBoxItem)ddlLanguage.SelectedItem).Name.ToString();
ResourceManager LocRM = new ResourceManager(strLanguage, typeof(MainWindow).Assembly);
lblNumber.Text = LocRM.GetString("strNumber");
lblMessage.Text = LocRM.GetString("strMessage");
btnSend.Content = LocRM.GetString("strSend");
menuItemFile.Header = LocRM.GetString("strMenuFile");
menuItemMainMenu.Header = LocRM.GetString("strMenuMainMenu");
menuItemClose.Header = LocRM.GetString("strMenuClose");
menuItemCheckConnection.Header = LocRM.GetString("strMenuConnection");
}
}
.xaml of Mainwindow.xaml:
<grid>
<Menu DockPanel.Dock="Top" Margin="0,0,0,434">
<MenuItem Header="_File" Name="menuItemFile">
<MenuItem Header="_Main Menu" Click="MenuItem_Click" Name="menuItemMainMenu"/>
<Separator/>
<MenuItem Header="Close" Click="MenuItem_Close" Name="menuItemClose"/>
</MenuItem>
<MenuItem Header="Monitoring" IsEnabled="False">
<MenuItem Header="Ping" IsCheckable="True" Checked="menuPingCheck" Unchecked="menuPingUncheck"/>
<MenuItem Header="GSM" IsCheckable="True" Checked="menuGsmCheck" Unchecked="menuGsmUncheck"/>
</MenuItem>
<MenuItem Header="Check Connection" Click="menuItemCheckConnection_Click" Name="menuItemCheckConnection"/>
<ComboBox x:Name="ddlLanguage" SelectionChanged="ddlLanguage_SelectionChanged" Height="23" Width="80">
<ComboBoxItem IsEnabled="False" IsSelected="True" Content="Language"/>
<ComboBoxItem x:Name="English" Content="English"/>
<ComboBoxItem x:Name="Czech" Content="Cesky"/>
</ComboBox>
<MenuItem Header="_Language" x:Name="menuLangTest" IsEnabled="True" Visibility="Hidden">
<MenuItem Header="Czech" x:Name="menuCzech" Checked="menuItemCzechCheck" Unchecked="menuItemCzechUnche"/>
<MenuItem Header="English" x:Name="menuEng" Checked="menuItemEngCheck" Unchecked="menuItemEngUnche"/>
</MenuItem>
</Menu>
<Ellipse Fill="#000000" Stroke="#000000" Margin="10,0,0,347" x:Name="ellGSM" HorizontalAlignment="Left" Width="38" Height="38" VerticalAlignment="Bottom" Visibility="Hidden"/>
<Ellipse Fill="#000000" Stroke="#000000" Margin="204,0,0,347" Name="ellCol" HorizontalAlignment="Left" Width="38" Height="38" VerticalAlignment="Bottom" Visibility="Hidden"/>
<TextBox x:Name="txtNumber" MaxLength="13" HorizontalAlignment="Left" Height="26" TextWrapping="Wrap" VerticalAlignment="Top" Width="310" Margin="10,153,0,0" PreviewTextInput="txtNumber_PreviewTextInput"/>
<TextBox x:Name="txtMessage" MaxLength="1300" HorizontalAlignment="Left" Height="100" TextWrapping="Wrap" VerticalAlignment="Top" Width="310" Margin="10,250,0,0"/>
<Button x:Name="btnSend" Cursor="Hand" Content="Send!" HorizontalAlignment="Left" VerticalAlignment="Top" Width="310" Margin="10,407,0,0" Height="52" FontSize="36" Click="btnSend_Click"/>
<TextBlock Grid.Column="0" Name="lblNumber" Text="Number" Margin="10,122,213,321" FontSize="16"/>
<TextBlock Grid.Row="0" Grid.Column="0" Name="lblMessage" Text="Message" Margin="10,217,213,224" FontSize="16"/>
<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="GSM" VerticalAlignment="Top" Margin="53,46,0,0" Name="txtblockGsm" Visibility="Hidden"/>
<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="PING" VerticalAlignment="Top" Margin="172,46,0,0" Name="txtblockPing" Visibility="Hidden"/>
<Image HorizontalAlignment="Left" Name="imgGsm" Height="42" VerticalAlignment="Top" Width="49" Margin="271,40,0,0" Source="C:\Users\lvrabel\source\repos\SMS Vrána\SMS Vrána\Images\GsmGreen.png" Visibility="Hidden"/>
<Label Content="1300" HorizontalAlignment="Left" Margin="287,345,0,0" VerticalAlignment="Top" Height="25" Name="lblLeftCharacters" />
<Label Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="280,84,0,0" Name="lblGsmSignal" Width="40"/>
</grid>
.xaml of Authentication.xaml what I would like to access in MainWindow.xaml.cs for changing language for that items as well. I just would like to change language in whole application and no just for one window.
<Grid Margin="0,-17,0,0">
<Menu DockPanel.Dock="Top" Margin="0,17,0,432">
<MenuItem Header="File" FontSize="16" Name="menuItemFile">
<MenuItem Header="Back on Main Menu" Click="menuItemMainMenuClick"/>
<Separator/>
<MenuItem Header="Close" Click="menuItemCloseClick"/>
</MenuItem>
</Menu>
<Label Content="IP Adress" Name="lblIPAddress" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,210,0,0" Height="36" Width="101" FontSize="16"/>
<Label Content="Login" Name="lblLogin" HorizontalAlignment="Left" Margin="10,279,0,0" VerticalAlignment="Top" Height="31" Width="59" FontSize="16"/>
<Label Content="Password" Name="lblPassword" HorizontalAlignment="Left" Margin="10,343,0,0" VerticalAlignment="Top" Height="31" Width="75" FontSize="16"/>
<TextBox HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="292" Margin="10,251,0,0" Name="txtBoxIP" PreviewTextInput="txtBoxIP_PreviewTextInput"/>
<TextBox HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="292" Margin="10,315,0,0" Name="txtBoxLogin"/>
<PasswordBox HorizontalAlignment="Left" VerticalAlignment="Top" Width="292" Margin="10,379,0,0" PasswordChar="*" Name="pswBox" Height="23" />
<CheckBox Content="Remember me!" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,413,0,0" Name="CheckBoxRemCredts" Checked="CheckBoxRemCredts_Checked"/>
<Button Content="Login" Cursor="Hand" HorizontalAlignment="Left" Height="34" Margin="10,442,0,0" VerticalAlignment="Top" Width="292" Name="btnLoginAuth" Click="btnLoginAuth_Click" FontSize="18"/>
</Grid>
U can access the xaml code of any xaml until and unless they are in same project and same solution.
U just need to initialize the other xaml you want to use in your xaml.cs file.
For example, I have two xaml files Main.xaml and Login.xaml
So i can use Login view and its element in Main.xaml.cs
Syntax :
Main.xaml.cs
Login login=new Login();
Now you can access any control of Login page provided u give name to every element you use.
eg : login.TextBlockEmail.Text="yourtext";
UPDATED ANSWER :
Hi, I got what you are looking for. Your whole idea is to achieve Localization where all your views associated with the main view shall update.
There are multiple approaches to achieve this, here i am sharing links which may help you :
https://www.codeproject.com/Articles/22967/WPF-Runtime-Localization
https://www.codeproject.com/Articles/35159/WPF-Localization-Using-RESX-Files
https://social.msdn.microsoft.com/Forums/vstudio/en-US/4a7f049b-f1b7-4982-874f-f9ecfe3d9140/how-to-change-the-language-in-my-view-wpf?forum=wpf
c# - localization - changing language in wpf app
What you ask for is possible - surely you can get reference to some control in the backend file (.xaml.cs) and then assign it to some variable that is visible to both classes.
However this is better done by using events. You define some event on your main class that should be visible to every window, and then subscribe to it in the code for each window.

WPF ObservableCollection not updating in view [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am working on a project using MVVM where a user connects to a database server and is presented a list of database areas on that server to use. For the list of areas, I am using a ComboBox that has ItemSource bound to an ObservableCollection.
ComboBox ItemsSource="{Binding AreaCollecton}" SelectedItem="{Binding SelectedArea}"
In the ViewModel, I have the ObservableCollection defined like this:
private ObservableCollection<string> areaCollection = new ObservableCollection<string>();
public ObservableCollection<string> AreaCollection
{
get { return areaCollection; }
set{
areaCollection = value;
OnPropertyChanged("AreaCollection");
}
}
The collection is updated when a server name is entered and a Connect button is clicked. The Click event will poll the server entered for what areas are available and return the list to the ViewModel.
public void Connect()
{
areaCollection.Clear();
serverConnection = new ServerConnect();
areaList = serverConnection.getAreaList(server);
//areaList is defined as a Dictionary<string, string>
foreach(KeyValuePair<string, string> area in areaList)
{
areaCollection.Add(area.Key);
}
OnPropertyChanged("AreaCollection");
}
I put a break point at the foreach in the Connect method (invoked from the button click event) and as I stepped through I saw areaCollection being updated from areaList as expected. However, once it completed building areaCollection, nothing is showing in the ComboBox on the View.
I am fairly new to WPF and MVVM so I have a feeling there is something small I am missing here. I've looked through some of the other posts here relating to similar issues, and have implemented the suggestions there but it still is not updating the View so I am not sure what I am missing. Any help is greatly appreciated.
EDIT: I'm adding the full XAML in case that is more helpful than just the ComboBox line.
<Grid Height="400" Width="400" VerticalAlignment="Center" HorizontalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="1.5*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.ColumnSpan="2" HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="lblPrompt" TextWrapping="Wrap" Text="Enter Laboratory login and connection info:" FontSize="18" />
<Label Style="{StaticResource ConnectLabel}" Grid.Row="1" Grid.Column="0" Content="Server:" />
<TextBox Style="{StaticResource ConnectTextBox}" Grid.Row="1" Grid.Column="1" x:Name="txtbxServerName" TextWrapping="Wrap" TabIndex="3" Text="{Binding Server}" />
<Button Style="{StaticResource ConnectButton}" Grid.Row="2" Grid.Column="1" x:Name="btnConnect" Content="Connect" FontSize="14">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:CallMethodAction TargetObject="{Binding}" MethodName="Connect" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
<Label Style="{StaticResource ConnectLabel}" Grid.Row="3" Grid.Column="0" Content="Area:" />
<ComboBox Style="{StaticResource ConnectCombo}" Grid.Row="3" Grid.Column="1" x:Name="txtbxAreaName" TabIndex="4" ItemsSource="{Binding AreaCollecton}" SelectedItem="{Binding SelectedArea}" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<ei:CallMethodAction TargetObject="{Binding}" MethodName="AreaConnect" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
<Label Style="{StaticResource ConnectLabel}" Grid.Row="4" Grid.Column="0" Content="Lab Username:" />
<TextBox Style="{StaticResource ConnectTextBox}" Grid.Row="4" Grid.Column="1" x:Name="txtbxUsername" TextWrapping="Wrap" TabIndex="1" Text="{Binding Username}" />
<Label Style="{StaticResource ConnectLabel}" Grid.Row="5" Grid.Column="0" Content="Lab Password:" />
<PasswordBox Margin="2" Grid.Row="5" Grid.Column="1" x:Name="pwdbxPassword" TabIndex="5" PasswordChanged="PasswordBox_PasswordChanged" />
<Button Style="{StaticResource ConnectButton}" Grid.Row="6" Grid.Column="1" x:Name="btnLogin" Content="Login" FontSize="14">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:CallMethodAction TargetObject="{Binding}" MethodName="Login" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
<TextBlock Grid.Row="7" Grid.ColumnSpan="2" HorizontalAlignment="Center" Margin="10" TextWrapping="Wrap" FontSize="14" Text="{Binding Status}" />
</Grid>
At first glance, i could see the Combox itemSource name is different than what you are setting in code behind. Shouldn't it be "AreaCollection" instead of "AreaCollecton". Let me know if solves the issue.

Hiding flyout of a button

I got my DataTemplate for items and within this DataTemplate I have such code:
<Button x:Name="DoneButton"
Style="{StaticResource ButtonStyle1}"
BorderThickness="1"
Margin="0,0,20,0"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Grid.Column="2"
Grid.Row="1"
Width="50"
Height="50"
>
<Image Source="Images/WPIcons/checked.png" Width="30" Height="30" Margin="-10,0,-10,0" />
<Button.Flyout>
<Flyout x:Name="myFly">
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" x:Uid="myNote" Text="Note: " Style="{StaticResource myText}" />
<TextBox Grid.Row="1" TextWrapping="Wrap" AcceptsReturn="True" Height="40" x:Name="note" Text="{Binding RecentNote, Mode=TwoWay}" Style="{StaticResource TextBoxStyle1}"/>
<Button x:Name="CompletedButton"
Command="{Binding CompletedCommand}"
CommandParameter="{Binding}"
Style="{StaticResource ButtonStyle1}"
BorderThickness="1"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Grid.Row="2"
Click="CompletedButton_Click"
Content="Done"
MinWidth="80"
Height="40"
/>
</Grid>
</Flyout>
</Button.Flyout>
</Button>
After the flyout for the item has been called and user put his data in it I want to hide this flyout as soon as user hits the "Done" button (x:Name="CompletedButton").
I tried to do that in code behind like:
private void CompletedButton_Click(object sender, RoutedEventArgs e)
{
Button button = (Button)sender;
Grid grid = (Grid)VisualTreeHelper.GetParent(button);
Flyout fly = (Flyout)VisualTreeHelper.GetParent(grid);
fly.Hide();
}
But I get cast exception with that I can't cast ContentPresenter to Flyout so I guess it's not the way I look for.
How I can hide this flyout?
I resolved it with creating global DependencyObject on the page. So when you click the button it keeps it and I can call its flyout to hide() from button within this flyout. A bit ugly but works like a charm.

WPF create button with custom content template

I have application in WPF where I need to create a number of buttons with the same content layout. It is currently defined in the Window as:
<Button Grid.Row="0" Grid.Column="0" Margin="4" >
<Button.Content>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="0.85*" />
<RowDefinition Height="0.25*" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" TextAlignment="Center" Text="Primary Text that can wrap" TextWrapping="Wrap" FontSize="14.667" />
<TextBlock Grid.Row="1" TextAlignment="Left" Text="smaller text" FontSize="10.667" />
</Grid>
</Button.Content>
</Button>
What I would ideally like to do is change that to:
<controls:MultiTextButton Grid.Row="0" Grid.Column="0" PrimaryText="Primary Text that can wrap" SecondaryText="smaller text" />
Rightly or wrongly I've created the following class:
public class MultiTextButton : Button
{
public static readonly DependencyProperty PrimaryTextProperty = DependencyProperty.Register("PrimaryText", typeof(String), typeof(MultiTextButton));
public static readonly DependencyProperty SecondaryTextProperty = DependencyProperty.Register("SecondaryText", typeof(String), typeof(MultiTextButton));
static MultiTextButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MultiTextButton), new FrameworkPropertyMetadata(typeof(MultiTextButton)));
}
public string PrimaryText
{
get { return (string)GetValue(PrimaryTextProperty); }
set { SetValue(PrimaryTextProperty, value); }
}
public string SecondaryText
{
get { return (string)GetValue(SecondaryTextProperty); }
set { SetValue(SecondaryTextProperty, value); }
}
}
I'm now unsure of how to set the 'template' to display the content in the format as the original code in the Window. I've tried:
<ControlTemplate x:Key="MultiTextButtonTemplate" TargetType="{x:Type controls:MultiTextButton}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="0.85*" />
<RowDefinition Height="0.25*" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" TextAlignment="Center" Text="{Binding PrimaryText}" TextWrapping="Wrap" FontSize="14.667" />
<TextBlock Grid.Row="1" TextAlignment="Left" Text="{Binding SecondaryText}" FontSize="10.667" />
</Grid>
</ControlTemplate>
but in Blend and Visual Studio the button is not rendered.
You're after TemplateBinding:
<TextBlock Grid.Row="0" TextAlignment="Center" Text="{TemplateBinding PrimaryText}" TextWrapping="Wrap" FontSize="14.667" />
<TextBlock Grid.Row="1" TextAlignment="Left" Text="{TemplateBinding SecondaryText}" FontSize="10.667" />
This binding is for use specifically in control templates -- it refers to a dependency property in the control. (Note that a regular binding, by contrast, refers to a property in the current DataContext.)
Edit
To make it look like a button, copy the default control template for Button and replace its ContentPresenter with something like below:
<ContentControl Margin="2"
HorizontalAlignment="Center"
VerticalAlignment="Center"
RecognizesAccessKey="True"
ContentTemplate="{TemplateBinding ContentTemplate}">
<ContentControl.Content>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="0.85*" />
<RowDefinition Height="0.25*" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" TextAlignment="Center" Text="{Binding PrimaryText}" TextWrapping="Wrap" FontSize="14.667" />
<TextBlock Grid.Row="1" TextAlignment="Left" Text="{Binding SecondaryText}" FontSize="10.667" />
</Grid>
</ContentControl.Content>
</ContentControl>

Categories