我正在使用WPF类库,而不是应用程序。这是我在c#中制作的Label的示例,我想使用XAML对其进行“样式化”。
private void CreateElement(int i)
{
UIElementOut[i] = new Label();
var uiElement = (Label)UIElementOut[i];
uiElement.HorizontalAlignment = HorizontalAlignment.Center;
uiElement.VerticalAlignment = VerticalAlignment.Center;
uiElement.FontFamily = new FontFamily(FFontInput[i]);
uiElement.FontSize = Convert.ToDouble(FontSizeIn[i]);
uiElement.Content = TextIn[i];
Brush BgBrushColor = new SolidColorBrush(RGBAToMediaColor(FBgCol[i]));
Brush FgBrushColor = new SolidColorBrush(RGBAToMediaColor(FFgCol[i]));
uiElement.Background = BgBrushColor;
uiElement.Foreground = FgBrushColor;
Uri uri = new Uri("Styles/LabelStyle.xaml", UriKind.Relative);
StreamResourceInfo info = Application.GetContentStream(uri);
System.Windows.Markup.XamlReader reader = new System.Windows.Markup.XamlReader();
ResourceDictionary myResourceDictionary = (ResourceDictionary)reader.LoadAsync(info.Stream);
Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
Style myLabelStyle = myResourceDictionary["LabelStyle"] as Style;
uiElement.Style = myLabelStyle;
}
Run Code Online (Sandbox Code Playgroud)
为此,我有包含我的LabelStyle的ressourcedictionnary,所有内容都可以正常编译。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" …Run Code Online (Sandbox Code Playgroud) 参考:Ookii.Dialogs.Wpf.VistaOpenFileDialog:
我正在尝试像这样设置 Ookii 的 VistaOpenFileDialog 初始目录:
VistaOpenFileDialog vfb = new VistaOpenFileDialog();
vfb.Multiselect = true;
vfb.Title = "pouet";
vfb.RestoreDirectory = false;
vfb.InitialDirectory = @"C:\Users\";
if (vfb.ShowDialog() ?? false)
{
this.Dispatcher.Invoke(DispatcherPriority.Send, new Action(delegate
{
for (var i = 0; i < vfb.FileNames.Length; i++)
{
FileDisplay.Add(vfb.FileNames[i]);
}
}));
}
}
private void AddFiles_Click(object sender, RoutedEventArgs e)
{
t = new Thread(new ThreadStart(AddFileDialog));
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
Run Code Online (Sandbox Code Playgroud)
但是无论我尝试什么都不起作用,对话框一开始永远不会被初始目录打开,也永远不会被初始目录重新打开。我仍然可以毫无问题地选择文件夹的文件。
我已经尝试了以下(作为测试):
vfb.InitialDirectory = @"C:\Users\";
vfb.InitialDirectory = "C:\\Users\\";
vfb.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop).ToString();
Run Code Online (Sandbox Code Playgroud)
也试过 restoredirectory true 或 false …
我正在尝试为按钮创建样式,特别是更改 IsMouseOver 样式(必须通过控件模板覆盖它)。我正在尝试这段代码(我必须使用 c#,没有 xaml),但按钮只是变成黄色,IsMouseOver 没有做任何事情。
private void CreateElement(int i)
{
UIElementOut[i] = new Button();
var uiElement = (Button)UIElementOut[i];
uiElement.Width = 100;
uiElement.Height = 100;
uiElement.VerticalAlignment = VerticalAlignment.Center;
uiElement.HorizontalAlignment = HorizontalAlignment.Center;
uiElement.Content = TextIn[i];
uiElement.FontFamily = new FontFamily(FFontInput[i]);
uiElement.FontSize = Convert.ToDouble(FontSizeIn[i]);
Style MyButtonStyle = new Style();
ControlTemplate templateButton = new ControlTemplate(typeof(Button));
FrameworkElementFactory elemFactory = new FrameworkElementFactory(typeof(Button));
elemFactory.Name = "myButton";
elemFactory.SetValue(Button.BackgroundProperty, Brushes.Yellow);
templateButton.VisualTree = elemFactory;
elemFactory.AppendChild(new FrameworkElementFactory(typeof(ContentPresenter)));
Trigger templateTrigger = new Trigger { Property = Button.IsPressedProperty, Value = true };
templateTrigger.Setters.Add(new …Run Code Online (Sandbox Code Playgroud)