binding:text block to listbox wpf - wpf

I am new to WPF. I have this code
<Window x:Class="ElementBinding.MultipleBindings"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MultipleBindings" Height="320" Width="300">
<Grid>
<Slider Name="sliderFontSize" Margin="0,12,6,243"
Minimum="1" Maximum="40" Value="10"
TickFrequency="1" TickPlacement="TopLeft">
</Slider>
<TextBox Name="txtContent" Margin="0,44,0,208">Color should change here</TextBox>
<TextBlock Margin="3,150,3,3" Name="lblSampleText"
FontSize="{Binding ElementName=sliderFontSize, Path=Value}"
Text="{Binding ElementName=txtContent, Path=Text,Mode=TwoWay}"
Foreground="{Binding ElementName=lstColors, Path=SelectedItem.Tag}" >
Multiple Bindings
</TextBlock>
<ListBox Height="54" HorizontalAlignment="Left" Margin="12,90,0,0" Name="lstColors" VerticalAlignment="Top" Width="120" >
<ListBoxItem>Green</ListBoxItem>
<ListBoxItem>Red</ListBoxItem>
<ListBoxItem>Blue</ListBoxItem>
</ListBox>
</Grid>
</Window>
Text block will not appear if I select an item in list box. I think problem is in 'SelectedItem.Tag'. How can I resolve this?

You are correct. It should be Path=SelectedItem.Content:
<TextBlock Margin="3,150,3,3" Name="lblSampleText"
FontSize="{Binding ElementName=sliderFontSize, Path=Value}"
Text="{Binding ElementName=txtContent, Path=Text,Mode=TwoWay}"
Foreground="{Binding ElementName=lstColors, Path=SelectedItem.Content}" >

some advices
dont use just Margin to arrange your controls, look at Layout with Panels(Grid,DockPanel, and so on)
dont use the fontsize and a slider to create something like a zoom, better use LayoutTransform with ScaleTransform
at least Shakti is right the binding should be to SelectedItem.Content
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Slider Name="sliderFontSize" Grid.Row="0" TickPlacement="TopLeft"
Minimum="1" Maximum="5" Value="1"
TickFrequency="1">
</Slider>
<TextBox Name="txtContent" Grid.Row="1">Color should change here</TextBox>
<TextBlock Grid.Row="3" Name="lblSampleText"
Text="{Binding ElementName=txtContent, Path=Text,Mode=TwoWay}"
Foreground="{Binding ElementName=lstColors, Path=SelectedItem.Content}" >
<TextBlock.LayoutTransform>
<ScaleTransform ScaleX="{Binding ElementName=sliderFontSize, Path=Value}" ScaleY="{Binding ElementName=sliderFontSize, Path=Value}"/>
</TextBlock.LayoutTransform>
</TextBlock>
<ListBox Height="54" HorizontalAlignment="Left" Name="lstColors" VerticalAlignment="Top" Width="120" Grid.Row="2">
<ListBoxItem>Green</ListBoxItem>
<ListBoxItem>Red</ListBoxItem>
<ListBoxItem>Blue</ListBoxItem>
</ListBox>
</Grid>

Related

Dropshadow appearing around textblock itself instead of the individual text characters

In my code, I made a DataTemplate in a SurfaceListBox. I added a drop shadow to the stack panel as well as to the textblock within the stack panel. When I run it, the drop shadow on the textblock appears on the textblock UI element itself instead of the individual characters of text. I am wondering why this is happening and if there is a way to fix it, so the drop shadow does appear on the text.
<DataTemplate>
<StackPanel Background="WhiteSmoke" Height="190" Width="190">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="140"/>
<RowDefinition Height="50"/>
</Grid.RowDefinitions>
<Image VerticalAlignment="Top" HorizontalAlignment="Center" Height="140" Width="140" Stretch="Fill" Source="{Binding ImagePath}" />
<TextBlock Grid.Row="1" Text="{Binding Name}" Background="#9FCC19" Width="190" Height="50" TextAlignment="Center" VerticalAlignment="Center" Foreground="WhiteSmoke"
FontFamily="Segoe" FontSize="20" >
<TextBlock.Effect>
<DropShadowEffect ShadowDepth="2" RenderingBias="Performance"/>
</TextBlock.Effect>
</TextBlock>
</Grid>
<StackPanel.Effect>
<DropShadowEffect ShadowDepth="2"/>
</StackPanel.Effect>
</StackPanel>
</DataTemplate>
You need to get rid of the Background color of the TextBlock. You can put a Rectangle behind the TextBlock to achieve the same effect.
<DataTemplate>
<StackPanel Background="WhiteSmoke" Height="190" Width="190">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="140"/>
<RowDefinition Height="50"/>
</Grid.RowDefinitions>
<Image VerticalAlignment="Top" HorizontalAlignment="Center" Height="140" Width="140" Stretch="Fill" Source="{Binding ImagePath}" />
<Rectangle Fill="#9FCC19" Grid.Row="1" Width="190" Height="50"/>
<TextBlock Grid.Row="1" Text="{Binding Name}" Width="190" Height="50" TextAlignment="Center" VerticalAlignment="Center" Foreground="WhiteSmoke"
FontFamily="Segoe" FontSize="20" >
<TextBlock.Effect>
<DropShadowEffect ShadowDepth="2" RenderingBias="Performance"/>
</TextBlock.Effect>
</TextBlock>
</Grid>
<StackPanel.Effect>
<DropShadowEffect ShadowDepth="2"/>
</StackPanel.Effect>
</StackPanel>
</DataTemplate>

TextBlock TextWrapping not wrapping #2

OK... so this solution doesn't help
XAML is here
<ListBox ItemsSource="{Binding Path=ContentItems}" Background="White" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="2" Height="Auto" Width="Auto">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<Label VerticalAlignment="Center" Margin="0,0,0,5">Start</Label><svl:TimeEditor Value="{Binding Path=FormatedStart}" Width="87" HorizontalAlignment="Left" Margin="2,8" Name="dtpStart" FontSize="12" Height="25" VerticalAlignment="Center" />
<Label VerticalAlignment="Center" Margin="0,0,0,5">End</Label><svl:TimeEditor Value="{Binding Path=FormatedEnd}" Width="87" HorizontalAlignment="Left" Margin="2,8" Name="dtpEnd" FontSize="12" Height="25" VerticalAlignment="Center" />
</StackPanel>
<TextBlock Grid.Row="1" TextWrapping="Wrap" Name="tbText" Text="{Binding Path=Data}"></TextBlock>
</Grid>
<Grid Grid.Column="1" Visibility="Collapsed">
</Grid>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The following will helps for text wrapping:
<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled">
Well your TextBlock does not need to wrap since your specifying Width as Auto for it's ColumnDefinition which allows it to take all the Width it needs to fit Content even at the cost of overflowing. You either need to set the Column's Width to "*" to allow the TextWrapping to kick in when requested width exceeds allowable or manually force a MaxWidth on it using a Binding like
<TextBlock Name="tbText" Grid.Row="1" MaxWidth="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}, Path=ActualWidth}" Text="{Binding Path=Data}" TextWrapping="Wrap" />

Listbox resizing automatically, button doesn't

I've got a ListBox with CheckBoxes in it which resizes automatically when I resize the Window.
I found the CheckedListBox XAML code on StackOverflow I think and I didn't modify it, and I'd like the same thing for all the UI elements.
I've read something about setting Anchors. As I already said, I didn't do anything to the XAML code, neither did I set any Anchors?
Is there anything in the XAML code which I'm not seeing which is responsible for the resizing?
<Grid>
<TextBox Height="23" HorizontalAlignment="Left" Margin="12,28,0,0" Name="textBox1" VerticalAlignment="Top" Width="238" />
<Label Content="Profilname" Height="28" HorizontalAlignment="Left" Margin="12,0,0,0" Name="label1" VerticalAlignment="Top" />
<ListBox ItemsSource="{Binding datasource}" Margin="12,57,12,41" Name="checkedListBox1" Opacity="0.7">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Path=Item}" IsChecked="{Binding IsChecked}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Content="save" Margin="12,302,12,12" Name="button3" Click="button3_Click" />
</Grid>
Thanks in advance!
You are doing it like a Canvas, but you are using a Grid.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label Content="Profilname" Name="label1" Grid.Row="0" />
<TextBox Name="textBox1" Grid.Row="1" />
<ListBox ItemsSource="{Binding datasource}" Name="checkedListBox1" Grid.Row="2" >
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Path=Item}" IsChecked="{Binding IsChecked}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Content="save" Name="button3" Click="button3_Click" Grid.Row="3" />
</Grid>
See now everything resizes.
You can add margin and padding now as desired for styling.

textblock in user control TextWrapping not wrapping

I created user control with the textblock. But it will not wrap. This user control servers as a listboxitem.
<Grid x:Name="MainGrid" Height="Auto" Width="Auto">
<StackPanel Orientation="Horizontal">
<Image Height="50" Width="100" Stretch="Uniform" Name="image1" Source="{Binding Path=VideoImageUrl}" Margin="12,12,13,84" MouseLeftButtonDown="image1_MouseLeftButtonDown" MouseEnter="image1_MouseEnter" MouseLeave="image1_MouseLeave" />
<StackPanel Orientation="Vertical" >
<TextBlock TextWrapping="Wrap" Height="Auto" HorizontalAlignment="Left" Name="titleTextBox"
Text="{Binding Path=Title, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}"
VerticalAlignment="Center" Width="Auto" FontSize="14" FontWeight="SemiBold" />
<StackPanel Orientation="Vertical" x:Name="StackPanelDetails">
<TextBlock Height="Auto" HorizontalAlignment="Left" Name="desciptionTextBox"
TextWrapping="Wrap"
Text="{Binding Path=Desciption, Mode=OneWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}"
VerticalAlignment="Center" Width="Auto" />
<Line />
<ItemsControl x:Name="CustomItemsSource" ItemsSource="{Binding Path=LinksList}" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock>
<Hyperlink NavigateUri="{Binding Path=TopicUrl}" RequestNavigate="Hyperlink_RequestNavigate" >
<TextBlock Text="{Binding Path=TopicName}" />
</Hyperlink>
</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</StackPanel>
</StackPanel>
</Grid>
A ListBox's default template does not automatically limit the width of its items, but instead uses a ScrollViewer, which shows a horizontal scrollbar when an item is wider than the ListBox.
You can remove the ScrollViewer by replacing the ListBox's Template:
<ListBox ...>
<ListBox.Template>
<ControlTemplate>
<StackPanel IsItemsHost="True"/>
</ControlTemplate>
</ListBox.Template>
...
</ListBox>
Another important thing to note is that a StackPanel in the top-level Grid won't properly resize the contained elements. In the following simplified example the text in the TextBlock is not wrapped because the containing StackPanel simply does not resize it as you expect:
<Grid>
<StackPanel Orientation="Horizontal">
<Image ... />
<Text TextWrapping="Wrap" Text=... />
</StackPanel>
</Grid>
This way it works as you expect:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Image ... />
<TextBlock Grid.Column="1" TextWrapping="Wrap" Text=... />
</Grid>

Wpf Bind View to ViewModel add to wpf window

I have a view that contains a ItemsControl with some textblocks inside to display the name and other information. in my window I am adding the view to the window as follows and in the code behind of the window i am binding the datacontext of the view to the view model in the MainWindow Loaded event as follows ViewOwnerSideBar.DataContext = viewModel The application compiles but when I run it I dont get data? I checked my viewmodel and I do have data in my collection that I am returning. Does anyone have any good examples of how to do this. I am going to have a sidebar view and a main view on the right displaying the details of the owner.
This is my View
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources/ColorsAndBrushes.xaml"/>
<ResourceDictionary Source="/Resources/DefaultStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<DockPanel >
<ScrollViewer VerticalScrollBarVisibility="Auto" >
<ItemsControl Width="250"
VerticalAlignment="Stretch"
BorderThickness="0"
ItemsSource="{Binding Path=AllOwners}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Margin="2">
<Border Margin="2 2 0 0"
CornerRadius="4"
Background="Gray"
Opacity=".5" />
<Border BorderBrush="{StaticResource redBrush}"
BorderThickness="2"
CornerRadius="4"
Background="White"
Margin="0 0 2 2"
Padding="3">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Grid.ColumnSpan="2"
FontWeight="Bold"
Text="{Binding FullName}" />
<TextBlock Grid.Row="1"
Text=" FirstName: " />
<TextBlock Grid.Row="1"
Grid.Column="1"
Text="{Binding FirstName}" />
<TextBlock Grid.Row="2"
Text=" Email: " />
<TextBlock Grid.Row="2"
Grid.Column="1"
Text="{Binding Email}" />
</Grid>
</Border>
<Button Style="{StaticResource openButton}" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</DockPanel>
This is my window
<DockPanel>
<v:HeaderTopBar DockPanel.Dock="Top"></v:HeaderTopBar>
<!--<uc:SearchBar DockPanel.Dock="Top" />-->
<StatusBar DockPanel.Dock="Bottom">
<StatusBarItem DockPanel.Dock="Right">
<Slider x:Name="zoomSlider"
Width="125"
Value="1"
Minimum=".5"
Maximum="2" />
</StatusBarItem>
<StatusBarItem DockPanel.Dock="Right">
<TextBlock>Zoom:</TextBlock>
</StatusBarItem>
<StatusBarItem>
<TextBlock Text="{Binding StatusText}" />
</StatusBarItem>
</StatusBar>
<Expander DockPanel.Dock="Left"
ExpandDirection="Right"
IsExpanded="True"
BorderThickness="0 1 1 1"
BorderBrush="Gray"
Margin="0 2 0 0"
Padding="2">
<Expander.Header>
<TextBlock Text="Contacts"
FontSize="14"
FontWeight="Bold">
<TextBlock.LayoutTransform>
<RotateTransform Angle="90" />
</TextBlock.LayoutTransform>
</TextBlock>
</Expander.Header>
<v:OwnerSideBar/>
</Expander>
<TabControl x:Name="tabs"
Grid.Column="2"
Margin="5 0">
<TabControl.LayoutTransform>
<ScaleTransform ScaleX="{Binding ElementName=zoomSlider,
Path=Value}"
ScaleY="{Binding ElementName=zoomSlider,
Path=Value}" />
</TabControl.LayoutTransform>
</TabControl>
</DockPanel>
Firstly, ensure that the AllOwners collection you are binding to is an ObservableCollection.
Also, check the Output window in Visual Studio when executing, look for First chance exceptions being caught. This will be a clue as to where your binding problem will be.
I like to put a textblock on the View bound to the Items.Count property on the ItemsControl so that you can see if it is binding and not rendering anything or not binding correctly.
Give the ItemsControl a name, then put a textblock in:
<TextBlock Text="{Binding ElementName=itemControl1,Path=Items.Count}/>
This might be something you already looked at, but you might have a binding typo, did you check your output window to see if you get any trace messages?
And I guess I'm blind but I don't see where your usercontrol is in the xaml of the second entry.

Resources