我有以下代码:
MainWindow.xaml
<Window x:Class="SampleApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
DataContext="{Binding Employee}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="200" />
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="ID:"/>
<Label Grid.Row="1" Grid.Column="0" Content="Name:"/>
<TextBox Grid.Column="1" Grid.Row="0" Margin="3" Text="{Binding EmpID}" />
<TextBox Grid.Column="1" Grid.Row="1" Margin="3" Text="{Binding EmpName}" />
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
Employee.cs
namespace SampleApplication
{
public class Employee
{
public Employee()
{
EmployeeDetails employeeDetails = new EmployeeDetails();
employeeDetails.EmpID = 123;
employeeDetails.EmpName = "ABC";
}
}
public class EmployeeDetails
{ …Run Code Online (Sandbox Code Playgroud) 问题:TextBox当我在里面输入大文本时,我无法在键入内部时看到我的指针TextBox
描述:
TextBox输入文本时,如果输入的文本很大,则应启用滚动.ScrollViewer仅显示TextBox高度内的内容ScrollViewer码:
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="Scroll Content Inside Textbox" Style="{StaticResource PhoneTextNormalStyle}" Margin="25,0,180,0"/>
</StackPanel>
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ScrollViewer Height="200"
VerticalAlignment="Top">
<TextBox x:Name="txtBody"
Width="200"
AcceptsReturn="True"
/>
</ScrollViewer>
</Grid>
</Grid>
Run Code Online (Sandbox Code Playgroud) 我有以下内容ControlTemplate:
<ContentControl Content="{Binding ContentViewModel}">
<ContentControl.Resources>
<DataTemplate DataType="{x:Type viewModel:FilledContentViewModel}">
<usercontrols:FilledMainWindow x:Name="MainContent" />
</DataTemplate>
<DataTemplate DataType="{x:Type viewModel:EmptyContentViewModel}">
<!-- todo: Something like a StartPage here -->
</DataTemplate>
</ContentControl.Resources>
</ContentControl>
Run Code Online (Sandbox Code Playgroud)
这非常有效,直到视图模型尝试将ContentViewModel属性从 1更改FilledContentViewModel为新的FilledContentViewModel,然后 的内容ContentControl不会刷新。如果从EmptyContentViewModel到FilledContentViewModel或相反的方式切换,它就会起作用。
当然,仅更新现有的所有内容FilledContentViewModel也是一种解决方案。但我认为它可能会很快变得混乱,而只需使用ViewModel正确的上下文创建一个新的并切换它会更优雅。
有谁知道有什么方法可以让内容刷新DataTemplate吗?
我在我的项目中使用 Mahapps Metro 主题。我想创造一个有图像的TabControl地方。TabItemMahapps 主题提供了在选择 TabItem 时更改 TabItem 文本的颜色TabItem等。我想将此前景色绑定到我的图像。我有这个问题的解决方案,但我认为它不正确。
“坏”代码(但它有效):
<TabItem>
<TabItem.HeaderTemplate>
<DataTemplate>
<Grid Margin="0,5,0,0">
<TextBlock x:Name="myTextBlock" />
<Rectangle Width="28.947" Height="25" Fill="{Binding ElementName=myTextBlock, Path=Foreground}">
<Rectangle.OpacityMask>
<VisualBrush Stretch="Fill" Visual="{StaticResource appbar_image_gallery}" />
</Rectangle.OpacityMask>
</Rectangle>
</Grid>
</DataTemplate>
</TabItem.HeaderTemplate>
</TabItem>
Run Code Online (Sandbox Code Playgroud) 我遇到了绑定TextBox IsEnabled属性的问题.我在这个论坛上看过一些帖子,根据这些帖子,下面的代码应该起作用(至少这是我的想法).但是,当我运行应用程序时,IsNumberEnabled 属性上的调用只执行一次 - 就在加载相应的视图之前.有人可以帮我一把.谢谢.
XAML:
<Textbox Text="{Binding Path=Number, Mode=TwoWay}" IsEnabled="{Binding Path=IsNumberEnabled}" ... />
Run Code Online (Sandbox Code Playgroud)
模型视图:
public bool IsNumberEnabled
{
get { return ... condition ....; } }
Run Code Online (Sandbox Code Playgroud) 我想将多个类型的集合绑定到在画布中显示的ItemsControl.到目前为止,我的代码如下:
<ItemsControl ItemsSource="{Binding Path=Objects}">
<ItemsControl.Resources>
<DataTemplate DataType="self:CalibrationRectangle">
<Rectangle Fill="{Binding Path=Color, Converter={StaticResource ColorToBrushConverter}}" Width="{Binding Width}" Height="{Binding Height}" />
</DataTemplate>
<DataTemplate DataType="self:PolygonVM">
<Polygon Fill="{Binding Path=Color, Converter={StaticResource ColorToBrushConverter}}" Points="{Binding Points}" />
</DataTemplate>
</ItemsControl.Resources>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Background="Red" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<!--<ItemsControl.ItemTemplate>
<DataTemplate DataType="self:CalibrationRectangle">
<Rectangle Fill="{Binding Path=Color, Converter={StaticResource ColorToBrushConverter}}" Width="{Binding Width}" Height="{Binding Height}" />
</DataTemplate>
</ItemsControl.ItemTemplate>-->
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Canvas.Top" Value="{Binding Path=Y}" />
<Setter Property="Canvas.Left" Value="{Binding Path=X}" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
在此代码状态中,资源中的DataTemplates被完全忽略,而对象则表示为字符串.相反,我使用注释掉的行并在ItemsControl.ItemsTemplate中定义DataTemplate它的工作原理.但是,由于我需要多个DataTemplates(每种类型一个),这不是解决方案.
这里有什么问题?为什么资源中的DataTemplates不起作用?
任何想法都赞赏:)
<StackPanel Name="StpAddDel" Orientation="Horizontal" HorizontalAlignment="Right" Margin="5">
<RadioButton Name="rdbactive" GroupName="actinact" VerticalAlignment="Center" Margin="5,0" Width="50" Height="15" Foreground="Blue">
<RadioButton.Style>
<Style TargetType="{x:Type RadioButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RadioButton}">
<Grid>
<Image Source="c:\image.png" Width="32" Height="32"/>
<ContentPresenter/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</RadioButton.Style>
</RadioButton>
<RadioButton Name="rdbinactive" GroupName="actinact" VerticalAlignment="Center" Margin="5,0" Width="60" Height="15" Foreground="Blue">
<RadioButton.Style>
<Style TargetType="{x:Type RadioButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RadioButton}">
<Grid>
<Image Source="c:\image.png" Width="32" Height="32"/>
<ContentPresenter/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</RadioButton.Style>
</RadioButton>
<Button Name="BtnAdd" Height="20" Width="20" Margin="5,0" Template="{StaticResource AddImgBtnTemplate}" />
<Button Name="BtnDel" Height="20" Width="20" Margin="5,0" …Run Code Online (Sandbox Code Playgroud) 如果没有选择或只选择一个项目,如何禁用listboxitem上下文菜单?
ListBox有一个SelectedItems属性,但它是只读的,你不能绑定它.
<ListBox ItemsSource="{Binding Items}" SelectionMode="Extended">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="GOGO" />
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" HorizontalAlignment="Center" VerticalAlignment="Center" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud) 我一直在寻找这个,找到了一些与Winforms相关的解决方案,有些人甚至只是说WPF中很难实现,但这些帖子都很老了.
如果我有一个标准ListBox,声明为:
<ListBox
x:Name="listBox"
HorizontalAlignment="Left"
Height="240"
Margin="401,68,0,0"
VerticalAlignment="Top"
Width="345"
SelectionChanged="listBox_SelectionChanged"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
Grid.ColumnSpan="2"/>`
Run Code Online (Sandbox Code Playgroud)
并以编程方式:
System.ComponentModel.BindingList<string> listItems = new System.ComponentModel.BindingList<string>();
listBox.ItemsSource = listItems;
Run Code Online (Sandbox Code Playgroud)
有没有办法将这些字符串包裹在ListBox?
wpf ×8
xaml ×8
c# ×7
datatemplate ×2
itemscontrol ×1
listbox ×1
mvvm ×1
scrollviewer ×1
wpf-controls ×1