小编leb*_*ero的帖子

在组合框中使用转换器绑定项目

我有一个组合框,它绑定到数据表列,如下所示:

ComboBox.DataContext = DataDataTable;                
ComboBox.DisplayMemberPath = DataDataTable.Columns["IDNr"].ToString();
Run Code Online (Sandbox Code Playgroud)

列中的IDNr始终以4个字母开头,后跟ID号(例如BLXF1234).我需要在没有字母的情况下在Combobox中显示项目(我需要在组合框中显示1234).

所以我写了一个转换器:

class IDPrefixValueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value != null)
        {
            string s = value.ToString();
            if (s.Contains("BL"))
            {
                return s.Substring(4);
            }
            else
            {
                return s;
            }
        }
        return "";
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }       
Run Code Online (Sandbox Code Playgroud)

不,我怎么能告诉组合框使用转换器来显示项目?我在Xaml中试过这个:

ItemsSource="{Binding}" 
DisplayMemberPath="{Binding Converter={StaticResource IDPrefixValueConverter}}"
Run Code Online (Sandbox Code Playgroud)

但仍然没有工作...任何想法?谢谢

c# wpf xaml converter

30
推荐指数
1
解决办法
2万
查看次数

从wpf中的单独文件加载控件样式

我在Windows.Resources中添加了以下样式

<Window.Resources>
...
<!--A Style that extends the previous TextBlock Style-->
<!--This is a "named style" with an x:Key of TitleText-->
<Style BasedOn="{StaticResource {x:Type TextBlock}}"
   TargetType="TextBlock"
   x:Key="TitleText">
<Setter Property="FontSize" Value="26"/>
<Setter Property="Foreground">
 <Setter.Value>
  <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
    <LinearGradientBrush.GradientStops>
      <GradientStop Offset="0.0" Color="#90DDDD" />
      <GradientStop Offset="1.0" Color="#5BFFFF" />
    </LinearGradientBrush.GradientStops>
  </LinearGradientBrush>
  </Setter.Value>
</Setter>
</Style> 
...
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)

我的xaml代码中有很多样式,我想将每个组件样式保存到一个额外的文件(而不是外部文件)..例如,与TextBlocks相关的所有样式都应该在一个名为的文件中 TextBlockStyles.xaml

我怎么能在wpf中这样做?

如何链接项目中的样式?

提前致谢

.net c# wpf

13
推荐指数
3
解决办法
2万
查看次数

绑定到父控件中的属性

我有以下情况:

Window
  |_Grid
      |_ListView (MainProductGrid)
           |_View
               |_GridView
                    |_GridViewColumn
                             |_CellTemplate
                                     |_DataTemplate
                                             |_Label (LabelID)
Run Code Online (Sandbox Code Playgroud)

现在,我想在LabelID中显示ListView中行的索引.所以我做了以下:

<ListView ItemsSource="{Binding Path=ProductItems}" AlternationCount="{Binding Path=ProductItems.Count}">
...
Run Code Online (Sandbox Code Playgroud)

对于标签我有以下内容:

<Label x:Name="LabelID" 
       Content="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, 
       Path=(ListView.AlternationIndex)}"/>
Run Code Online (Sandbox Code Playgroud)

但它的LabelID只显示0 ..所以我认为TemplatedParent没有指向ListView控件..所以我怎样才能纠正绑定指向"上层父",在我的情况下是ListView?

提前致谢

################

更新:这是完整的xaml ...

<Grid x:Name="mainGrid">
        <ListView  ItemsSource="{Binding Path=ProductItems}" AlternationCount="{Binding Path=ProductItems.Count}" x:Name="MainProductGrid">
                <ListView.View>
                    <GridView AllowsColumnReorder="False">
                    <GridViewColumn x:Name="gvc" Header="id" Width="auto">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Label Content="{Binding (ListView.AlternationIndex),RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}}"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>

                    <GridViewColumn Header="Product name" DisplayMemberBinding="{Binding Path=ProductName}"  Width="auto" />
                    </GridView>
                </ListView.View>
            </ListView>
    </Grid>
Run Code Online (Sandbox Code Playgroud)

wpf xaml

12
推荐指数
2
解决办法
3万
查看次数

在webbrowser控件中保存图像,而无需从Internet重新下载它们

是否可以将webbroswer控件中的图像直接保存到硬盘,而无需再次从Internet下载?

假设我导航到一个有15张图片的网站.它们都在我的webbrowser中查看,但是如何在不需要下载的情况下保存它们呢?

c# browser image save

10
推荐指数
1
解决办法
1万
查看次数

c#抓住剪贴板更改,如果窗体最小化到托盘,将无法工作

我在使用此链接上的"Catch Clipboard Events代码"时遇到问题:

剪贴板事件C#

只有当表单停留在前台时,代码才能生效,而不是最小化到托盘但是:如果你添加一个notifyicon并将表单最小化到托盘并将showintaskbar变为false(这样你在托盘中只有一个图标),程序不会再抓住任何剪贴板更改...即使你最大化表单,它也不会再工作...你必须重新启动程序..

关于如何解决这个问题的任何想法!即使表格最小化到托盘中,我怎样才能捕捉到剪贴板的变化!

任何帮助真的很感激......

谢谢

c# clipboard notifyicon

6
推荐指数
1
解决办法
1375
查看次数

将 xelement 加载到数据表中

我有以下 xml 文件,其中包含很多关于公司分支机构的信息..(这只是一个例子)。

我真正需要的是仅将来自 Branch1 的数据加载到数据表中(与我的 xml 文件具有相同的结构,因此数据表完全没有问题)..

我正在使用 c#,我想做的是 linq,但我对 linq 一无所知……我的问题是:我如何将 xml 中的条目作为数据表行读取,以便我可以将其复制到我的数据表中?

我现在有:

XElement main = XElement.Load("branches.xml");
IEnumerable<XElement> elList =
from el in main.Descendants("branch").Where(ex=>ex.Attribute("name").Value=="Branch1")
select el;
//this will return me the element where name =Branch1
//now, how would i only load this entry into my datatable ??
//this won`t work
branchesDataTable.ReadXml(XElement el in elList);
Run Code Online (Sandbox Code Playgroud)

任何帮助真的很感激..

<?xml version="1.0" encoding="utf-8"?>
<branches>
<branch name="Branch1">
    <address>Street 1, 1234, NY</address>
    <tel>0123456789</tel>
    <director>James</director>
</branch>   
<branch name="Branch2">
    <address>Street 2, 4567, NY</address>
    <tel>9876543210</tel>
    <director>Will</director>
</branch> …
Run Code Online (Sandbox Code Playgroud)

c# xml linq-to-xml

3
推荐指数
1
解决办法
7163
查看次数

剪贴板侦听器事件被调用两次

我想保存剪贴板中的更改.所以我注册了我的应用程序以获取剪贴板发生的所有更改.

运用

    [DllImport("User32.dll")]
    protected static extern bool AddClipboardFormatListener(int hwnd);
Run Code Online (Sandbox Code Playgroud)

然后

protected override void WndProc(ref Message m)
    {
switch (m.Msg)
        {
            case WM_CLIPBOARDUPDATE:
                OnClipboardChanged();
                break;
             ...
        }
     }

private void OnClipboardChanged()
{
    if (Clipboard.ContainsText())
        {
         MessageBox.Show(Clipboard.GetText().ToString());
        }
}
Run Code Online (Sandbox Code Playgroud)

问题是:当从visual studio或firefox等应用程序复制文本时,OnClipboardChanged()函数有时会被调用两次或三次.

我认为那些应用程序会将数据写入不同格式的剪贴板,这就是为什么函数不止一次被调用.但是我如何防止保存相同的数据,因为OnClipboardChanged()被多次调用?

c# clipboard

3
推荐指数
1
解决办法
1729
查看次数

Type 对象属性的 TypeConverter

我需要在属性网格中正确显示对象。我的课程是这样的:

public class PropertyItem
{
    public PropertyDescription PropertyDescription { get; set; }

    [Description("the value"), Browsable(true)]
    public object Value { get; set; }

    public PropertyItem(PropertyDescription propertyDescription, object value)
    {
        PropertyDescription = propertyDescription;
        Value = value;
    }

    public override string ToString()
    {
        return this.PropertyDescription.Name + ": " + PropertyDescription.Type + ": " + Value;
    }
}
Run Code Online (Sandbox Code Playgroud)

Value是类型object,不能更改。ThePropertyDescription的类型为 the Value,这可以是任何内容 ( string, int, bool...)

当我设置SelectedObject我的时PropertyGridValue总是被禁用。

我该如何编写 aTypeConverter …

c# typeconverter winforms

3
推荐指数
1
解决办法
3933
查看次数

从设置中将textblock内容设置为字符串

我的表单中有2个文本块.这样的事情:

<TextBlock Name="applicationname" Text="{binding applicationname?}"/>
<TextBlock Name="applicationname" Text="{binding settings.stringVersionNumber}"/>
Run Code Online (Sandbox Code Playgroud)

我想设置第一个文本块内容自动显示应用程序名称,另一个显示保存在应用程序设置中的字符串.

我应该使用"wpf代码隐藏"来更改值,还是我可以直接在xaml中绑定文本块?

c# wpf xaml

2
推荐指数
1
解决办法
1370
查看次数

标签样式未更新windowstate已更改

我试图基于WindowState属性更新Label的内容..我有以下内容:

<Window x:Class="WpfApplication1.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">
    <Window.Resources>
        <Style x:Key="StyleMinMax" TargetType="{x:Type Label}">
            <Setter Property="TextElement.FontSize" Value="22" />
            <Setter Property="TextElement.FontWeight" Value="SemiBold" />
            <Setter Property="VerticalAlignment" Value="Stretch" />
            <Setter Property="HorizontalAlignment" Value="Stretch" />
            <Setter Property="VerticalContentAlignment" Value="Center" />
            <Setter Property="Foreground" Value="LightSlateGray" />
            <Style.Triggers>
                <Trigger Property="Window.WindowState" Value="Normal">
                    <Setter Property="Content" Value="1-Normal" />
                </Trigger>
                <Trigger Property="Window.WindowState" Value="Maximized">
                    <Setter Property="Content" Value="2-Maximized" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>

    <Grid>
        <Label Style="{StaticResource StyleMinMax}"></Label>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

应用程序启动时标签内容将具有1-Normal,但是当我最大化我的应用程序时,内容不会改变..任何想法?

提前致谢

c# wpf xaml

1
推荐指数
1
解决办法
708
查看次数

标签 统计

c# ×9

wpf ×5

xaml ×4

clipboard ×2

.net ×1

browser ×1

converter ×1

image ×1

linq-to-xml ×1

notifyicon ×1

save ×1

typeconverter ×1

winforms ×1

xml ×1