我想将保证金设置为所有页面的默认值.对于我使用的高度
<System:Double x:Key="Height">20</System:Double>
Run Code Online (Sandbox Code Playgroud)
但保证金是'0,2,0,0'我必须使用的类型是什么?
我不想使用样式和设置器.
给出以下代码:
<Window x:Class="WpfApplication76.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:col="clr-namespace:System.Collections;assembly=mscorlib"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<CollectionViewSource x:Key="myCol">
<CollectionViewSource.Source>
<col:ArrayList>
<ListBoxItem>Uno</ListBoxItem>
<ListBoxItem>Dos</ListBoxItem>
<ListBoxItem>Tres</ListBoxItem>
</col:ArrayList>
</CollectionViewSource.Source>
</CollectionViewSource>
</Window.Resources>
<Grid>
<ListBox ItemsSource="{StaticResource myCol}" />
<ListBox ItemsSource="{Binding Source={StaticResource myCol}}" />
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
在这个例子中,
<ListBox ItemsSource="{StaticResource myCol}" />
Run Code Online (Sandbox Code Playgroud)
给我一个错误,抱怨它无法绑定到"CollectionViewSource"对象.
但是另一个列表框:
<ListBox ItemsSource="{Binding Source={StaticResource myCol}}" />
Run Code Online (Sandbox Code Playgroud)
结合完美.
所以我的问题是为什么一个工作而另一个不工作?最后,是不是两个ItenSources都设置为相同的"CollectionViewSource"对象?
谢谢.
我有WPF 4应用程序,它有许多UserControl,它们在子控件中共享样式.所以我在我的App.xaml中创建了许多样式.例如:
<Style x:Key="ViewTitle" TargetType="{x:Type Border}">
<Setter Property="BorderBrush" Value="LightGray" />
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="VerticalAlignment" Value="Top" />
</Style>
Run Code Online (Sandbox Code Playgroud)
然后在我的UserControls中,我使用StaticResource来引用它们,例如:
<Border Style="{StaticResource ViewTitle}">
<TextBlock Margin="6,3,4,5" FontSize="18" FontWeight="Bold" Foreground="White" HorizontalAlignment="Left" Text="Host Management" />
</Border>
Run Code Online (Sandbox Code Playgroud)
一切正常.问题是Visual Studio 2010 RC在StaticResource引用下放置了一个蓝色波浪形,并说:无法解析资源"ViewTitle".
那是怎么回事?这是Visual Studio的问题还是我做错了什么?
如果我有标签:
<Label Content="{StaticResource Foo}" />
Run Code Online (Sandbox Code Playgroud)
有没有办法在xaml中附加*?
我正在寻找类似的东西:
<Label Content="{StaticResource Foo, stringformat={0}*" />
Run Code Online (Sandbox Code Playgroud)
我从资源字典中放置控件的内容,因为该应用程序支持多种语言.我想知道是否可以在xaml中附加*,这样我就不必创建一个事件,然后在事件触发时附加它.
在资源字典中,我有:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:system="clr-namespace:System;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<system:String x:Key="Foo">Name</system:String>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
在我的窗口我有:(我合并了最后一本字典)
<Label Content="{StaticResource 'Foo'}" />
Run Code Online (Sandbox Code Playgroud)
并显示名称
我希望标签显示Name*而不仅仅是Name
也许有可能通过一种风格实现这一目标.
我想对IValueConverter窗口的标题进行绑定,以便在活动项目更改时进行更改.问题是值转换器是一个静态资源,以后只加载几行:
<Window x:Class="MyProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyProject"
Height="600" Width="800" VerticalAlignment="Stretch"
Title="{Binding ActiveProject, Converter={StaticResource windowTitleConverter}}, UpdateSourceTrigger=PropertyChanged">
<Window.Resources>
<local:MainWindowTitleConverter x:Key="windowTitleConverter"/>
</Window.Resources>
<!-- Rest of the design -->
</Window>
Run Code Online (Sandbox Code Playgroud)
然后是转换器的定义:
public class MainWindowTitleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null) return "Programme"; else return "Programme: " + (value as string);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Run Code Online (Sandbox Code Playgroud)
这崩溃了,大概是因为StaticResource尚未加载(我想不出任何其他原因),因为没有转换器它工作正常.但是,我无法改变顺序.我试图把它放在一个<Window.Title> …
我有一个名为Customer2个属性的简单类.
然后我创建了一个只用一个名为Customers的公共属性命名的集合类
public Name {get;set;}
public LastName {get;set}CustomerList
public class CustomerList
{
public List<Customer> Customers { get; set; }
public CustomerList()
{
Customers = new List<Customer>();
Customers.Add(new Customer() { Name = "Foo", LastName = "Bar" });
Customers.Add(new Customer() { Name = "Foo1", LastName = "Bar1" });
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想在XAML中将此类用作静态资源.
<UserControl.Resources>
<customers:CustomerList x:Key="CustomersKey">
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)
然后在ListBox中使用它
<ListBox x:Name="lvTemplate" ItemsSource="{Binding Source={StaticResource CustomersKey}}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBox Text="{Binding Name}"/>
<TextBox Text="{Binding LastName}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
如果我在代码中设置ItemsSource behide,在实例化类之后,一切正常.如果我尝试从XAML设置它并且静态资源没有发生.即使我使用{Binding Path=Customer.Name}或者 …
在我的spring-mvc应用程序中,我正在提供一些静态资源.JavaScrips,CSS和图像得到正确的服务,但也有一些json文件无法提供.
所以我在浏览器中可以看到这个文件: http:// localhost:8080/path/to/resources/example.png
但是这个文件(在同一个目录中)我没有收到: http:// localhost:8080/path/to/resources/example.json
我明白了:
DEBUG: org.springframework.web.servlet.resource.ResourceHttpRequestHandler - No media type found for ServletContext resource [/resources/path/to/resources/example.json] - returning 404
Run Code Online (Sandbox Code Playgroud)
所以我假设我需要在配置中添加这个mediatype扩展(.json),但我找不到它.
请帮忙!
<local:LabelTemp x:Key="labelTemplate"/>
<DataTemplate x:Key="labelTemp">
<TextBlock Text="{Binding Converter={StaticResource labelTemplate},Path=Item.Items}"/>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮助我如何将上述Xaml代码写入Code Behind C#.我将此代码用于饼图LabelTemplate.
我想覆盖StaticResource在我自己的资源字典中的不同程序集的资源字典中配置的 a 。我尝试使用相同的密钥配置新资源,但没有成功。实际加载的资源来自所提到的程序集的资源字典。
出于演示目的,我将资源称为“MyResource”:
MyResourceDictionary.xaml:
<ResourceDictionary xmlns=...>
<!-- I use the same key as the original resource, from the other assembly -->
<DataTemplate x:Key="MyResource">
<!--My own implementation of that resource -->
</DataTemplate>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
应用程序.xaml
<Application x:Class=...>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- Resource dictionary from different assembly -->
<ResourceDictionary Source="pack://application:,,,/Assembly;component/ResourceDictionary.xaml"/>
<!-- My resource dictionary -->
<ResourceDictionary Source="pack://application:,,,/MyApplication;component/ResourceDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud) 我正在尝试正确设置我的风格。因此,我为所有常见样式属性创建了一个外部ResourceDictionary,在其中定义了一个默认字体系列,如下所示:
<FontFamily x:Key="Default.FontFamily">Impact</FontFamily>
Run Code Online (Sandbox Code Playgroud)
这样,当我更改这一行时,家庭在所有地方都会发生变化。
使用和引用 StaticResource
现在我想在没有定义任何其他内容的地方使用这个默认字体系列,这是在大多数地方(但不是全部)。但是,我想保留为任何使用此字体的地方定义其他字体系列的能力。因此,我使用了此处和此处找到的示例,并为组框标题显式定义了默认字体:
<StaticResource x:Key="GroupBox.HeaderFontFamily" ResourceKey="Default.FontFamily"/>
Run Code Online (Sandbox Code Playgroud)
我在TextBlock我的组框模板中使用它。
<Style x:Key="GroupBoxHeaderTextStyle" TargetType="{x:Type TextBlock}">
<Setter Property="FontFamily" Value="{StaticResource GroupBox.HeaderFontFamily}"/>
</Style>
Run Code Online (Sandbox Code Playgroud)
到目前为止,这是有效的。但是,一旦我添加另一行:
<StaticResource x:Key="GroupBox.HeaderFontFamily" ResourceKey="Default.FontFamily"/>
<StaticResource x:Key="FormLabel.FontFamily" ResourceKey="Default.FontFamily"/>
Run Code Online (Sandbox Code Playgroud)
我抛出这个异常:
异常:找不到名为“Hsetu.GroupBox.HeaderFontFamily”的资源。资源名称区分大小写。
所以我经历过,WPF 在后面跟着 a 时无法找到直接寻址的元素StaticResource(是的,这也适用于 StaticResources 以外的元素。例如,如果我尝试直接寻址字体系列,"Default.FontFamily"我会得到相同的错误,因为它先于一个StaticResource元素)
使用 DynamicResource 并引用 StaticResource
我尝试使用DynamicResource第二个示例中的建议,我提供了上面的链接:
<DynamicResource x:Key="GroupBox.HeaderFontFamily" ResourceKey="Default.FontFamily"/>
<DynamicResource x:Key="FormLabel.FontFamily" ResourceKey="Default.FontFamily"/>
<Style x:Key="GroupBoxHeaderTextStyle" TargetType="{x:Type TextBlock}">
<Setter Property="FontFamily" Value="{StaticResource GroupBox.HeaderFontFamily}"/>
</Style>
Run Code Online (Sandbox Code Playgroud)
这会引发以下错误:
ArgumentException:“System.Windows.ResourceReferenceExpression”不是属性“FontFamily”的有效值。
使用和引用 DynamicResource
在我的组框样式中使用DynamicResource仅更改了错误消息:
<DynamicResource x:Key="GroupBox.HeaderFontFamily" …Run Code Online (Sandbox Code Playgroud) staticresource ×10
wpf ×7
xaml ×4
binding ×3
.net ×2
c# ×1
datatemplate ×1
itemssource ×1
path ×1
resources ×1
silverlight ×1
spring-mvc ×1
styles ×1
wpf-controls ×1